MException

Creates an exception object that carries an identifier and a message.

Category: Programming > Programming Utilities

Syntax

ME = MException(errID, msg)

Builds an exception with the identifier errID and the message msg.

ME = MException(errID, msg, A1, ..., An)

Formats the message like sprintf with the extra arguments.

Description

Creating the object does not raise an error; use throw(ME) for that. Building an exception separately is useful to prepare it, add causes, or store it for later. Exceptions caught by catch ME are the same kind of object, so code can inspect ME.identifier to decide what to do and ME.message to show the user.

Input arguments

errID
Identifier of the form component:mnemonic, with no spaces.
msg
Message text, possibly with conversions such as %d and %s.

Output arguments

ME
An MException object with properties identifier, message, cause, stack and Correction.

Example

Building and throwing an exception

ME = MException('app:range', 'Value %d is too large', 99);
try
    throw(ME)
catch e
    disp(e.identifier)
    disp(e.message)
end
Output
app:range
Value 99 is too large

See also

throw rethrow error try, catch addCause