| MATLAB® | ![]() |
When your program detects a fault that will keep it from completing as expected or will generate erroneous results, you should halt further execution and report the error by throwing an exception. The basic steps to take are
Detect the error. This is often done with some type of conditional statement, such as an if statement that checks the output of the current operation.
Construct an MException object to represent the error. Add a message identifier string and error message string to the object when calling the constructor.
If there are other exceptions that may have contributed to the current error, you can store the MException object for each in the cause field of a single MException that you intend to throw. Use the addCause method for this.
Use the throw or throwAsCaller function to have the MATLAB® software issue the exception. At this point, MATLAB stores call stack information in the stack field of the MException, exits the currently running function, and returns control to either the keyboard or an enclosing catch block in a calling function.
![]() | Capturing Information About the Error | Responding to an Exception | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |