4

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.

2
  • There seems to be a big difference between the hypothetical tryif and catchif statements. For tryif I imagine it would not try the code, whereas with catchif it would always try, but not always catch. Are you after catchif? Commented Nov 15, 2013 at 18:12
  • @chappjc I always want to Do something, but just want to controll whether I am forgiving when it fails. The accepted answer seems to be exactly what I was looking for. Commented Nov 16, 2013 at 11:55

4 Answers 4

5

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".

Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

Imho it's as elegant as any other if/else block. You also wouldn't want to invent a forif or continueif etc.
@sebastian It is as elegant as any if else block, but not as elegant as any try catch block as this answer requires duplication. However from the other answer it seems there is a way around this.
1

The best I've been able to do is:

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

And then

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

Then, if desired for production-style deployments, avoid the constant condition checking like this:

function errorLogger(me)
%Error logging disabled for deployment

3 Comments

Well, except for the logging thing this is exactly the same solution as mine. An acknowledgement of that would have been nice.
It seems to me the goals of this problem are: (1) minimize code clutter, and especially reuse, at the point the error could occur; (2) provide the clean, and low-overhead options for switching between error handling modes. I have found that functionalizing the inner IF statement helps with both of these goals. Error logging is just an added bonus which is relevant to my current real world activities, and therefore shows up in all my thoughts. :)
Gave you +1, mostly for the comments that go with the answer. It is indeed nice to have a solution usable in 1 line, but I suppose I could just put the code by @A.Donda in a script. --- Sidenote, it is I plan to take advantage of isdeployed for setting the right condition automatically in most situations.
1

For the "tryif" functionality, you could assert on the first line of the try block:

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

Note that the "Do something" code will not be executed if loose==1.

For the "catchif" functionality, A.Donda's approach of checking loose~=1 on the first line of the catch block seems quite good.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.