Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional try catch statement in matlab

I am looking for an elegant way to use a conditional try catch statement.

I suppose it could look something like this:

tryif loose==1
% Do something, like loading the user preferences
catch %Or catchif?
% Hande it
end

So far I know that you can use try catch blocks to let your compiled code run, but force it to stop in a debug session with dbstop if caught error. Now I am basically looking for the opposite:

Normally I want the code to stop if unexpected situations occur (to guarantee the integrity of results) but want to be less strict about certain things sometimes when I am debugging.

like image 504
Dennis Jaheruddin Avatar asked Feb 14 '23 16:02

Dennis Jaheruddin


2 Answers

How about this:

try
  % Do something, like loading the user preferences
catch exception
  if loose ~= 1
    rethrow(exception)
  end
  % Handle it
end

I don't know about elegant ;-), but at least it avoids the duplication of "do something".

like image 135
A. Donda Avatar answered Feb 17 '23 05:02

A. Donda


I know one way to do it, though I would hardly call this elegant:

if loose == 1
  try
    % Do something, like loading the user preferences
  catch
    % Hande it
  end
else
  % Do something, like loading the user preferences
end
like image 44
Dennis Jaheruddin Avatar answered Feb 17 '23 07:02

Dennis Jaheruddin