MATLAB: How to replace the LASTERROR function in the MATLAB code with the try-catch exception method

MATLAB

In my code I am using the LASTERR and the LASTERROR functions to get the state of the last error using application execution.

Best Answer

This change has been incorporated into the documentation in Release 2010a (R2010a). For previous releases, read below for any additional information:
The use of the functions LASTERR and LASTERROR is no longer recommended for capturing the state of the last error. It is suggested to replace the use of these functions with the new style of try-catch statement that captures an MException object to represent the error. This is because LASTERROR and LASTERR functions kept track of only the one most recently thrown error. The state of any errors thrown before that was overwritten by newer errors. Also, this error state was globally accessible, which means that it could be unintentionally modified or destroyed either during an interactive session or by another function. As in the example below,
% This code will NOT work in future releases





try
aUserDefinedFunction;
catch
someFunction;
rethrow(lasterror);
end
if someFunction did not guard against modification of lasterror as seen above, and doSomeStuff threw an error, then the rethrow(lasterror) would
rethrow the wrong error. In the following document, the function LASTERROR is meant to include both LASTERROR and LASTERR functions.
Below are some suggestions of how to replace some of the most common uses of the functions LASTERR and LASTERROR with equivalent TRY-CATCH Exception errors:
1. The data that is available from the functions LASTERROR and LASTERR can also be accessed using the properties of the MException object created with the TRY-Catch block. These properties can then be used to:
a. rethrow the last error message. As an example, replace this code
% This code will NOT work in future releases
try
...
catch
rethrow(lasterr) % or rethrow(lasterror)
end
with
% This code will work




try
...
catch myException
rethrow(myException) % or throw(myException), throwAsCaller(myException)
end
b. modify the appearance of the displayed error message (like remove "Error using ==>" ) or the message string. As an example, replace this code
% This code will NOT work in future releases
try
...
catch
...
msg = strrep(lasterr,xlate('Error using ==>'));
error(msg);
...
end
with
% This code will work
try
...
catch myException
...
throw(myException)
end
c. use the last error message to throw a warning. As an example, replace this code:
% This code will NOT work in future releases
[prev, ID] = lasterr;
try
...
catch
...
lasterr('reset') % OR lasterr('') OR lasterr(prev,ID)
end
with
% This code will work
try
...
catch myException
warning(myException.identifier, '%s', myException.getReport('basic'));
end
2. The special case when the LASTERROR function is being used to get the error message thrown from a statement in the EVAL or the EVALC command can be replaced with the following equivalent try-catch syntax. As an example, replace this code
% This code will NOT work in future releases
eval('doStuffThatErrors;', 'myError=lasterror; doErrorHandlingStuff(myError)');
with
% This code will work
try
eval('doStuffThatErrors;');
catch myError
doErrorHandlingStuff(myError)
end
3. When used in a PARFOR loop, the name of the MException variable needs to be preallocated before the PARFOR. As an example, replace this code
% This code will NOT work in future releases
function testParfor
parfor i = 1:10
try
error('oups');
catch err
disp(err.getReport);
end
end
with
% This code will work
err = [ ];
parfor i = 1:10
try
error('oups');
catch err
disp(err.getReport);
end
end