Matlab conditional catch catch statement

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

I assume it might 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 blocks try catchto make your compiled code run, but make it stop in a debugging session using dbstop if caught error. Now I'm basically looking for the opposite:

Usually I want the code to stop if unforeseen situations arise (to guarantee the integrity of the results), but you want to be less strict on some things, sometimes when I debug.

+4
source share
4 answers

How about this:

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

;-), , , " -".

+4

, :

if loose == 1
  try
    % Do something, like loading the user preferences
  catch
    % Hande it
  end
else
  % Do something, like loading the user preferences
end
+1

, , :

try
    % Do something, like loading the user preferences
catch me
    errorLogger(me);
    %Handle the error
end

function errorLogger(me)
LOOSE = true;    
%LOOSE could also be a function-defined constant, if you want multiple uses.  
%    (See: http://blogs.mathworks.com/loren/2006/09/13/constants/)

if LOOSE
    %Log the error using a logger tool.  I use java.util.logging classes, 
    %but I think there may be better options available.
else
    rethrow(me);
end

, , :

function errorLogger(me)
%Error logging disabled for deployment
+1

"tryif" assert try:

try
    assert(loose==1)
    % Do something
catch err
    if strcmp(err.identifier,'MATLAB:assertion:failed'),
    else
    % Hande error from code following assert
    end
end

, "Do something" , loose==1.

"catchif" A.Donda loose~=1 catch .

+1

All Articles