0

I'm working with someone else's code and I am unfamiliar with try/catch so I made a small, similar example. On line 11, if I write error(''), it doesn't seem to catch the error and increase the index j. However, writing error(' ') or error('bad!') does.

So does having an error with an empty string ignore the error, or am I doing something wrong?

% Just a file to understand the Matlab command try/catch

M = 3;
j = 1;
k = [Inf, 5, 4];

while M>0
    try
        M = M-1
        u = k(j)
        if (isinf(u)||isnan(u)), error(''), end;
    catch
        j = j+1
    end
end
0

4 Answers 4

3

Yes, error('') and error([]) and error(struct([])) all do not actually display an error message and abort running code. I personally consider the use of the single string argument version of error to be bad practice in any real code. You should use always use both a 'MSGID' and a 'ERRMSG' when writing errors for your functions, e.g.

error('FunctionName:SubFunctionName:ErrorMSGID','Error message to be printed.')

Alternatively, you can use MException objects in conjuction with throw, rethrow, and throwAsCaller, which allow you to reuse error information. More here.

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

2 Comments

Thank you! It's good to have a confirmation for this. In the documentation for error it says: "All string input arguments must be enclosed in single quotation marks. If msgString is an empty string, the error command has no effect." I should have searched for "error" instead of "try/catch"! Thank you. :)
Rolled back since I was mistaken... sorry.
2

It is odd, but it's in the documentation for error, for the error('msgString') syntax:

All string input arguments must be enclosed in single quotation marks. If msgString is an empty string, the error command has no effect.

Similarly, if using the error(msgStruct) syntax:

If msgStruct is an empty structure, no action is taken and error returns without exiting the function.

Comments

0

if you have a look to the try documentation you can have an example.

Else want you want for your code it :

M = 3;
j = 1;
k = [Inf, 5, 4];

while M>0
    try
        M = M-1
        u = k(j)
        if (isinf(u)||isnan(u)), error(''), end;
    catch
        disp('I catch an error!');
        j = j+1
    end
end

Because If you never get an error in your code, it will never go in the catch. So by including error('');, it just to say, go execute the statement in the catch.

But you can just modify your code by replacing the error() by the statements into your catch like this :

while M>0
        M = M-1
        u = k(j)
        if (isinf(u)||isnan(u)), j = j+1, end;
end

EDIT

If you take a look in the documentation, you can found this :

%   ERROR(MSGSTRUCT) reports the error using fields stored in the scalar
%   structure MSGSTRUCT. This structure can contain these fields:
%
%       message    - Error message string
%       identifier - See MESSAGE IDENTIFIERS, below
%       stack      - Struct similar to the output of the DBSTACK function
%  
%   If MSGSTRUCT is an empty structure, no action is taken and ERROR
%   returns without exiting the program. If you do not specify the
%   stack, the ERROR function determines it from the current file and line.

So no action is taken as you can read. And nothing, so catch don't get any informations.

2 Comments

My question is about using error('') versus error('nonempty string'). The top case still doesn't go to the catch (for me) unless there is a nonempty string in the error. I was wondering if it's standard for error('') to make errors be ignored. I like the second example. Maybe I will have to edit the code to work like this, but right now I am trying to understand a code that is more complicated than my example (but similar).
@Catalina, Oh! You're right. I never experiment that. Because normally when I throw error, I use message or use it like this error();. Let me have a look on that. I'll come with another answer.
0

Not sure why you need it, but here is how it works.

error function does not throw an error with empty string or empty vector ([]) as an argument.

If you don't specify argument at all the error function itself generates the error "Not enough arguments". So it will go to catch.

Another way is to specify an empty structure as an argument.

s = struct();
error(s)

In this case, the error will be generated, but the code will not stop and in general flow you will hear no beep. In your case it should go to catch.

4 Comments

That's not an empty struct, it's a struct with no fields. You need to do s = struct([]); for a 0x0 struct array. Then it will not error.
I thought the question was to throw the error but have no output. This is exactly what error(struct()) do. error(struct([])) will not throw the error, same as error('').
Oh, I think the OP was just curious why error('') didn't throw an error, hence the discussions about empty structs being the same way. But since you point it out, I'd say you have a nice way of tripping an error with no message. :)
Although it does not make any sense to me. :))) I mean a purpose to do it.

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.