User`s guide

Desktop Tools and Development Environment
18-15
LASTERR and LASTERROR are better replaced by an identifier on the
CATCH block. See doc CATCH.
The value assigned here to variable 'x' might never be used.
This message appears when the code contains a catch statement that is never used.
TRY statement without a CATCH.
For example, suppose you want to read options from a file, options.txt, but it is
acceptable if that file is not present. You might write the following code, expecting the
read_options program to throw an error if the file is not present:
options = {};
try
options = read_options( ‘options.txt’ );
end
The problem with the preceding code is that the file might be present, but its permissions
may prevent the program from reading it. The program ignores the file, and potentially
confuses the user, who knows the file is there. Better code for accomplishing the task is
as follows, which assigns a structure value to the variable err if an error is thrown in the
try block. The structure value contains information about the error that was thrown.
try
options = read_options( ‘options.txt’ );
catch err
if strcmp( err.identifier, ‘Program:ReadOptions:NoOptionsFile’ )
options = {};
else
rethrow( err );
end
end
Using this code, if a problem other than the “file is missing” error occurs, MATLAB
reports the error to the user. For instance, MATLAB reports an error if the file format is
incorrect, or if the file has the wrong permissions.
If you feel comfortable ignoring the errors completely, it is probably best to use the try
statement with no catch statement, and suppress the M-Lint warnings that result.
You can suppress the warnings through the M-Lint preferences or by placing the %#ok
pragma at the end of the line that triggers the message. However, The MathWorks
suggests that if you suppress an M-Lint message, you include a comment in your code
indicating why you think it is appropriate to ignore the message.
For more information about the MException class, see the Error Handling section in the
MATLAB Programming Fundamentals documentation.