try/catch followed by return has the effect of returning to the calling routine when an error is detected, without executing any following code. try with no catch, or try/catch that does not have a return (or exit or quit or error()) in the catch, continues executing after the try/end even if an error occurred.
The ability to throw an empty MException object using THROW, RETHROW and THROWASCALLER is not available in MATLAB. To work around this issue, use a condition statement to bypass the use of an empty MException Object:
errobj = MyIsEqual(params)
if isempty(errobj)
% do nothing
else
throwAsCaller(errobj)
end
Using condition statements whenever an empty MException object is given, however, may affect the conciseness of the code.
This is the intended behavior of the LASTERR command.
When the CATCH command is used with an identifier, it does not change the status of the global LASTERR. Hence, we should use the identifier to check the error occured and not the LASTERR command.
Best Answer