Documentation Center

  • Trial Software
  • Product Updates

throwAsCaller (MException)

Throw exception as if from calling function

Syntax

throwAsCaller(exception)

Description

throwAsCaller(exception) throws an exception from the currently running function based on the exception input, a scalar object of the MException class. The MATLAB® software exits the currently running function and returns control to either the keyboard or an enclosing catch block in a calling function. Unlike the throw function, MATLAB omits the current stack frame from the stack field of the MException, thus making the exception look as if it is being thrown by the caller of the function.

In some cases, it is not relevant to show the person running your program the true location that generated an exception, but is better to point to the calling function where the problem really lies. You might also find throwAsCaller useful when you want to simplify the error display, or when you have code that you do not want made public.

Examples

The function klein_bottle, in this example, generates a Klein Bottle figure by revolving the figure-eight curve defined by XYKLEIN. It defines a few variables and calls the function draw_klein, which executes three functions in a try, catch block. If there is an error, the catch block issues an exception using either throw or throwAsCaller:

function klein_bottle(pq)
ab = [0 2*pi];
rtr = [2 0.5 1];
box = [-3 3 -3 3 -2 2];
vue = [55 60];
draw_klein(ab, rtr, pq, box, vue)

function draw_klein(ab, rtr, pq, box, vue)
clf
try
   tube('xyklein',ab, rtr, pq, box, vue);
   shading interp
   colormap(pink);
catch exception
   throw(exception)
%   throwAsCaller(exception)
end

Call the klein_bottle function, passing a vector, and the function completes normally by drawing the figure.

klein_bottle([40 40])

Call the function again, this time passing a scalar value. Because the catch block issues the exception using throw, MATLAB displays error messages for line 16 of function draw_klein, and for line 6 of function klein_bottle:

klein_bottle(40)
Error using klein_bottle>draw_klein (line 16)
Attempted to access pq(2); index out of bounds because numel(pq)=1.

Error in klein_bottle (line 6)
draw_klein(ab, rtr, pq, box, vue)

Run the function again, this time changing the klein_bottle.m file so that the catch block uses throwAsCaller instead of throw. This time, MATLAB only displays the error at line 6 of the main program:

klein_bottle(40)
Error using klein_bottle (line 6)
Attempted to access pq(2); index out of bounds because numel(pq)=1.

More About

expand all

Tips

There are four ways to throw an exception in MATLAB (see the list below). Use the first of these when testing the outcome of some action for failure and reporting the failure to MATLAB. Use one of the remaining three techniques to throw an existing exception.

  1. Test the result of some action taken by your program. If the result is found to be incorrect or unexpected, compose an appropriate message and message identifier, and pass these to MATLAB using the error function.

  2. Reissue the original exception by throwing the initial exception unmodified. Use the MException rethrow method to do this.

  3. Collect additional information on the cause of the error, store it in a new or modified exception, and issue a new exception based on that record. Use the MException addCause and throw methods to do this.

  4. Make it appear that the error originated in the caller of the currently running function. Use the MException throwAsCaller method to do this.

See Also

| | | | | | | |

Was this topic helpful?