Reputation: 171
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
Upvotes: 0
Views: 694
Reputation: 18484
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.
Upvotes: 3
Reputation: 19870
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.
Upvotes: 0
Reputation: 2359
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.
Upvotes: 0
Reputation: 30579
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.
Upvotes: 2