MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
end

id =
MATLAB:narginchk:notEnoughInputs

Text of the Error Message

An error message in MATLAB is a read-only character vector issued by the program code
and returned in the MException object. This message can assist the user in determining
the cause, and possibly the remedy, of the failure.

When throwing an exception, compose an appropriate error message and save it to the
MException object at the time you construct the object using the syntax

ME = MException(identifier, text)

If your message requires formatting specifications, like those available with the sprintf
function, use this syntax for the MException constructor:

ME = MException(identifier, formatstring, arg1, arg2, ...)

For example,

S = 'Accounts'; f1 = 'ClientName';
ME = MException('AcctError:Incomplete', ...
'Field ''%s.%s'' is not defined.', S, f1);

ME.message
ans =
Field 'Accounts.ClientName' is not defined.

When responding to an exception, you can extract the error message from the
MException object as follows:

try
surf
catch ME
msg = ME.message
end

msg =
Not enough input arguments.

26 Error Handling

Free download pdf