In the statement
try
doSomethingThatMayResultInError();
catch me
doSomethingWithTheError(me);
end
me
is an object of class MException that contains all information, including stack trace, of the error.
If you would like to add additional information to the error you're throwing, you can do it the following way:
try
doSomethingThatMayResultInError(a,b);
catch me
%# throw a more easy-to-understand error
error(['This use case, i.e. with length of a (%i) different from length of b (%i)'...
'is not supported. Error details: %s'], length(a), length(b), me.message)
end
Of course, you can make error handling more involved by adding e.g. switch/case statements with error identifiers (me.identifier
), although you should be advised that error identifiers have been changing a few times in the last few versions of Matlab.