MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
This example creates a function hello that requires one input argument. If the function
is called without inputs, MATLAB produces and error and suggests the input argument
"world" as a fix.
function hello(audience)
if nargin < 1
me = MException('MATLAB:notEnoughInputs','Not enough input arguments.');
aac = matlab.lang.correction.AppendArgumentsCorrection('"world"');
me = me.addCorrection(aac);
throw(me);
end
fprintf("Hello, %s!\n", audience);
end

When you call the function without an argument, MATLAB suggests a fix.

hello

Error using hello (line 6)
Not enough input arguments.

Did you mean:
>> hello("world")

Methods of the MException Class


There are several methods that you can use with the MException class. The names of
these methods are case-sensitive. See the MATLAB function reference pages for more
information.

Method Name Description
addCause Append an MException to the cause field of
another MException.
addCorrection Provide a suggested fix for the current
exception.
getReport Return a formatted message based on the
current exception.
MException.last Return the last uncaught exception. This is a
static function.
rethrow Reissue an exception that has previously been
caught.
throw Issue an exception.

26 Error Handling

Free download pdf