This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Exception Handling

Capture and retrieve data on causes of errors

MATLAB Language Syntax

try, catchExecute statements and catch resulting errors


MExceptionCapture error information


addCauseRecord additional causes of exception
getReportGet error message for exception
lastReturn last uncaught exception
rethrowRethrow previously caught exception
throwThrow exception
throwAsCallerThrow exception as if occurs within calling function


Exception Handling in a MATLAB Application

It is a good programming practice to include error checking in your programs to ensure reliable operation under all conditions. You can decide how your programs respond to different types of errors.

Capture Information About Exceptions

When a program throws an exception, information about what caused the error is captured in an MException object. If you catch the exception before your program aborts, you can access the information. You can also create MException objects that you use to throw exceptions in your code.

Throw an Exception

Detect conditions that keep your program from completing as expected or generate erroneous results, and then halt execution and throw an exception that reports the error.

Respond to an Exception

To handle an exception appropriately for a particular condition, capture information about the exception with a try/catch statement and then execute code to deal with the situation.