(shown here as point A). If all operations in the try block succeed, then execution skips
the catch block entirely and goes to the first line following the end statement (point B).
Specifying the try, catch, and end commands and also the code of the try and catch
blocks on separate lines is recommended. If you combine any of these components on the
same line, separate them with commas:
try, surf, catch ME, ME.stack, end
ans =
file: 'matlabroot\toolbox\matlab\graph3d\surf.m'
name: 'surf'
line: 54
NoteYou cannot define nested functions within a try or catch block.
The Try Block
On execution, your code enters the try block and executes each statement as if it were
part of the regular program. If no errors are encountered, MATLAB skips the catch block
entirely and continues execution following the end statement. If any of the try
statements fail, MATLAB immediately exits the try block, leaving any remaining
statements in that block unexecuted, and enters the catch block.
The Catch Block
The catch command marks the start of a catch block and provides access to a data
structure that contains information about what caused the exception. This is shown as the
variable ME in the preceding pseudocode. This data structure is an object of the MATLAB
MException class. When an exception occurs, MATLAB constructs an instance of this
class and returns it in the catch statement that handles that error.
You are not required to specify any argument with the catch statement. If you do not
need any of the information or methods provided by the MException object, just specify
the catch keyword alone.
The MException object is constructed by internal code in the program that fails. The
object has properties that contain information about the error that can be useful in
determining what happened and how to proceed. The MException object also provides
access to methods that enable you to respond to the exception. See the section on “The
MException Class” on page 26-5 to find out more about the MException class.
26 Error Handling