Finally Clause in Try-Catch
77 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Mohammad Abouali
il 18 Ago 2016
Commentato: deepak panday
il 2 Set 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 Commento
deepak panday
il 2 Set 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
Risposta accettata
Walter Roberson
il 18 Ago 2016
ME = [];
try
%do some stuff
catch ME
end
%close the open resources
if ~isempty(ME)
rethrow(ME);
end
1 Commento
Più risposte (1)
Guillaume
il 18 Ago 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 Commenti
Jan Siegmund
il 10 Mar 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
il 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.
Vedere anche
Categorie
Scopri di più su Error Handling in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!