Exception Handling in a MATLAB Application
No matter how carefully you plan and test the programs you write, they may not always run as smoothly as expected when executed under different conditions. It is always a good idea to include error checking in programs to ensure reliable operation under all conditions.
In the MATLAB® software, you can decide how your programs respond to different types of errors. You may want to prompt the user for more input, display extended error or warning information, or perhaps repeat a calculation using default values. The error-handling capabilities in MATLAB help your programs check for particular error conditions and execute the appropriate code depending on the situation.
When MATLAB detects a severe fault in the command or program it is running, it collects information about what was happening at the time of the error, displays a message to help the user understand what went wrong, and terminates the command or program. This is called throwing an exception. You can get an exception while entering commands at the MATLAB command prompt or while executing your program code.
Getting an Exception at the Command Line
If you get an exception at the MATLAB prompt, you have several options on how to deal with it as described below.
Determine the Fault from the Error Message
Evaluate the error message MATLAB has displayed. Most error messages attempt to explain at least the immediate cause of the program failure. There is often sufficient information to determine the cause and what you need to do to remedy the situation.
Review the Failing Code
If the function in which the error occurred is implemented as a MATLAB program file, the error message should include a line that looks something like this:
surf Error using surf (line 49) Not enough input arguments.
The text includes the name of the function that threw the error
surf, in this case) and shows the failing line number
within that function's program file. Click the line number; MATLAB opens the file and positions the cursor at the location in the
file where the error originated. You may be able to determine the cause of the
error by examining this line and the code that precedes it.
Step Through the Code in the Debugger
You can use the MATLAB Debugger to step through the failing code. Click the underlined
error text to open the file in the MATLAB Editor at or near the point of the error. Next, click the hyphen
at the beginning of that line to set a breakpoint at that location. When you
rerun your program, MATLAB pauses execution at the breakpoint and enables you to step through
the program code. The command
dbstop on error is also helpful
in finding the point of error.
See the documentation on Debug MATLAB Code Files for more information.
Getting an Exception in Your Program Code
When you are writing your own program in a program file, you can catch exceptions and attempt to handle or resolve them instead of allowing your program to terminate. When you catch an exception, you interrupt the normal termination process and enter a block of code that deals with the faulty situation. This block of code is called a catch block.
Some of the things you might want to do in the catch block are:
Examine information that has been captured about the error.
Gather further information to report to the user.
Try to accomplish the task at hand in some other way.
Clean up any unwanted side effects of the error.
When you reach the end of the catch block, you can either continue executing the program, if possible, or terminate it.
MException object to access
information about the exception in your program. For more information, see Respond to an Exception.
Generating a New Exception
When your program code detects a condition that will either make the program fail or yield unacceptable results, it should throw an exception. This procedure
Saves information about what went wrong and what code was executing at the time of the error.
Gathers any other pertinent information about the error.
Instructs MATLAB to throw the exception.
MException object to capture information about the
error. For more information, see Throw an Exception.