MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Exception Handling in a MATLAB Application


In this section...
“Overview” on page 26-2
“Getting an Exception at the Command Line” on page 26-2
“Getting an Exception in Your Program Code” on page 26-3
“Generating a New Exception” on page 26-4

Overview


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.

26 Error Handling

Free download pdf