try, catch
Execute statements and catch resulting errors
Syntax
trystatements
catchexception
statements
end
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
catch
blocks within atry
block, but you can nest completetry/catch
blocks.Unlike some other languages, MATLAB does not allow the use of a
finally
block withintry/catch
statements.