Finally Clause in Try-Catch

105 views (last 30 days)
Just making sure
MATLAB Try/Catch does not have the "finally" block? Correct?
I am simulating the behavior using something like this:
% Opening resources
% Doing stuff with the resources
% This is at the end of the try block so if the code reaches here
% this means that everything went well with no problem.
throw(MException('TryCatchFinally:noWorries', ...
['This exception can be ignored.' ...
'This is to just flag that the try block finished' ...
'without any error and can be used to simulate ' ...
'finally behavior'], ...
catch ME
% Closing all the opened resources
% rethrowing error if it is anything other than 'TryCatchFinally:noWorries'
if (~strcmp(ME.identifier,'TryCatchFinally:noWorries') )
However raising an exception could affect performance in cases where your code is called several times. The alternative to above code is the following but this second one requires to have the code that closes the resources twice (it's OK though if all I need to do is fclose(fid)):
% Opening resources
% Doing stuff with the resources
catch ME
% Closing all the opened resources;
% This is called only if any exception is raised in the try block.
% Closing all the opened resources.
What other method do you guys suggest?
  1 Comment
deepak panday
deepak panday on 2 Sep 2020
isError = false
%% this portion is executed even if error is not in the code segment
if isError
.%% write code here that need to go in error handling
%% write code here that need to go in finally portion

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 18 Aug 2016
ME = [];
%do some stuff
catch ME
%close the open resources
if ~isempty(ME)
  1 Comment
Mohammad Abouali
Mohammad Abouali on 18 Aug 2016
I like this one. Thanks

Sign in to comment.

More Answers (1)

Guillaume on 18 Aug 2016
Yes, there's no finally in matlab.
It remains to be seen if raising exceptions have an impact or not on execution speed. It's not something I've tested.
It certainly seems a bit subversive to use exceptions to trigger normal completion. Probably a better way is to use onCleanup which is even more bulletproof than a finally (if the try ... finally is the whole scope of the function) since it gets triggered even on an abort (CTRL+C):
function myfunc
%Opening resources
fid = fopen('somefile');
cleanfid = onCleanup(@() fclose(fid)); %called on completion of function, whether normally, by exception or abort
%do stuff with resource
Steven Lord
Steven Lord on 10 Mar 2020
If you pressed Ctrl-C while you were doing stuff with the resource in Guillaume's example, the myfunc function would exit. As long as you don't return the onCleanup object from myfunc, it will be destroyed (and the cleanup function executed) as the myfunc function exits.
Ideally you should prefer creating the onCleanup object before making the change you want to clean up (if possible, which it is in the warning suppression case and is not in the file closing case) or as soon afterwards as you can. Anything you ran between the fopen call and the onCleanup call, if it failed, would leave the bench.dat file open.
About the only way you may be able to get a function to terminate without triggering the onCleanup objects in its workspace is to forcibly kill MATLAB (including having MATLAB crash.) If MATLAB Goes Away the operating system should handle reclaiming memory, but it's not going to allow MATLAB to try executing the onCleanup object's destructor.

Sign in to comment.


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!