Issue
I want to catch an exception, but print the same message it would print if not caught (stack trace). How to do that?
I tried
>> myfunctionwitherror
Error using myfunctionwitherror (line 3)
myerror
>> try myfunctionwitherror; catch disp(lasterror), end
Warning: This try-catch syntax will continue to work in R2007b, but may be illegal or may mean something different in future releases of MATLAB.
See MATLAB R2007a Release Notes, "Warning Generated by try-catch" for details.
MException with properties:
identifier: ''
message: 'myerror'
cause: {0×1 cell}
stack: [0×1 struct]
>> try myfunctionwitherror, catch e getReport(e), end
Warning: This try-catch syntax will continue to work in R2007b, but may be illegal or may mean something different in future releases of MATLAB.
See MATLAB R2007a Release Notes, "Warning Generated by try-catch" for details.
Undefined function or variable 'e'.
How to accomplish this?
I am using 2016b
. I don't know why this message appears.
Solution
You are using 2007a syntax for try catch.
it was:
try,
statementA
catch,
statementB
end
and now is
try
statementA
catch e
statementB
end
But as you are writing one liners, you forgot the ;
so you are just confusing MATLAB about when the lines end, so its assuming you are doing
try
myfunctionwitherror
catch
e
getReport(e)
end
Just put the semicolons where they should be when you are using ambiguous one liners. Or write multiple lines. ;)
try; myfunctionwitherror; catch e; getReport(e); end;
If what you want is to display the error (without trowing an error) just
try; myfunctionwitherror; catch e; disp(e.message); end;
Answered By - Ander Biguri Answer Checked By - Marilyn (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.