MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

% 3) Store any information contributing to the error.
if nargin < 2
causeException = MException('MATLAB:notEnoughInputs','Not enough input arguments.');
baseException = addCause(baseException,causeException);


% 4) Suggest a correction, if possible.
if(nargin > 1)
exceptionCorrection = matlab.lang.correction.AppendArgumentsCorrection('1');
baseException = baseException.addCorrection(exceptionCorrection);
end


throw(baseException);
end


try
assert(isnumeric(idx),'MYFUN:notNumeric', ...
'Indexing array is not numeric.')
catch causeException
baseException = addCause(baseException,causeException);
end


if any(size(idx) > size(A))
msgID = 'MYFUN:incorrectSize';
msg = 'Indexing array is too large.';
causeException2 = MException(msgID,msg);
baseException = addCause(baseException,causeException2);
end


% 5) Throw the exception to stop execution and display an error
% message.
throw(baseException)
end
end


If you call the function without specifying an index, the function throws a detailed error
and suggests a correction:


A = [13 42; 7 20];
indexIntoArray(A)


Error using indexIntoArray (line 21)
Unable to index into array.


Caused by:
Not enough input arguments.


Did you mean:




indexIntoArray(A, 1)




If you call the function with a nonnumeric index array that is too large, the function
throws a detailed error.


Throw an Exception
Free download pdf