throwAsCaller
Raises an exception as if it came from the calling function.
Category: Programming > Programming Utilities
Syntax
throwAsCaller(ME)
Throws ME with the call stack trimmed so the error appears to originate in the caller.
Description
A function that validates its inputs usually wants the error attributed to the call that made the mistake, not to an internal helper. throwAsCaller removes the current function from the reported stack for that purpose. The identifier and message are unchanged.
Input arguments
ME- An
MExceptionobject.
Example
Reporting a bad argument to the caller
function r = safeSqrt(x)
if x < 0
throwAsCaller(MException('safe:neg', 'x must be nonnegative'));
end
r = sqrt(x);
end
disp(safeSqrt(16))
try
safeSqrt(-1)
catch err
disp(err.message)
end
Output
4
x must be nonnegative
Notes
MatLivre raises the error normally, so the reported location is not adjusted.