Finally Clause in Try-Catch

102 Ansichten (letzte 30 Tage)
Mohammad Abouali
Mohammad Abouali am 18 Aug. 2016
Kommentiert: deepak panday am 2 Sep. 2020
Just making sure
MATLAB Try/Catch does not have the "finally" block? Correct?
I am simulating the behavior using something like this:
% Opening resources
try
% 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'], ...
0));
catch ME
% Closing all the opened resources
% rethrowing error if it is anything other than 'TryCatchFinally:noWorries'
if (~strcmp(ME.identifier,'TryCatchFinally:noWorries') )
rethrow(ME);
end
end
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
try
% 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.
rethrow(ME);
end
% Closing all the opened resources.
What other method do you guys suggest?
  1 Kommentar
deepak panday
deepak panday am 2 Sep. 2020
isError=true
try
..
...
isError = false
throw(Me)
catch
%% 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
end
%% write code here that need to go in finally portion
end

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 18 Aug. 2016
ME = [];
try
%do some stuff
catch ME
end
%close the open resources
if ~isempty(ME)
rethrow(ME);
end
  1 Kommentar
Mohammad Abouali
Mohammad Abouali am 18 Aug. 2016
Bearbeitet: Mohammad Abouali am 18 Aug. 2016
I like this one. Thanks

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Guillaume
Guillaume am 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
end
  5 Kommentare
Jan Siegmund
Jan Siegmund am 10 Mär. 2020
ah, got it. but the above mentioned post by guillaume wont trigger on ctrl+c, right?
So the right approach would be
function myfunc
%Opening resources
fid = fopen('bench.dat', 'rt');
cleanfid = onCleanup(@() fclose(fid)); %called on completion of function, whether normally, by exception or abort
%do stuff with resource
end
, wouldn't it?
Where it would catch on issues and ctrl+c at the
%do stuff with resource
part.
Steven Lord
Steven Lord am 10 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by