try, catch
Execute statements and catch resulting errors
Syntax
trystatementscatchexceptionstatementsend
Description
try executes
the statements in the statements,
catch statements endtry block and catches resulting
errors in the catch block. This approach allows
you to override the default error behavior for a set of program statements.
If any statement in a try block generates an error,
program control goes immediately to the catch block,
which contains your error handling statements.
exception is an MException object
that allows you to identify the error. The catch block
assigns the current exception object to the variable in exception.
Both try and catch blocks
can contain nested try/catch statements.
Examples
Tips
You cannot use multiple
catchblocks within atryblock, but you can nest completetry/catchblocks.Unlike some other languages, MATLAB does not allow the use of a
finallyblock withintry/catchstatements.