| Products & Services | Industries | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
throwAsCaller(errRecord)
throwAsCaller(errRecord) throws an exception from the currently running M-file based on MException object errRecord. 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.
There are four ways to throw an exception in MATLAB. Use the first of these when testing the outcome of some action for failure and reporting the failure to MATLAB:
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 or assert function.
Use one of the remaining three techniques to resume an exception that is already in progress but has been temporarily suspended in a try-catch statement:
Reissue the original exception by returning the initial error record unmodified. Use the MException rethrow method to do this.
Collect additional information on the cause of the error, store it in a new or modified error record, and issue a new exception based on that record. Use the MException addCause and throw methods to do this.
Set or modify the stack field of a new or existing error record to make it appear that the error originated in the caller of the currently running function. Use the MException throwAsCaller method to do this.
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 errRecord
throw(errRecord)
% throwAsCaller(errRecord)
endCall 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 at 16 Attempted to access pq(2); index out of bounds because numel(pq)=1. Error in ==> klein_bottle at 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 at 6 Attempted to access pq(2); index out of bounds because numel(pq)=1.
try, catch, error, assert, MException, throw(MException), rethrow(MException), addCause(MException), getReport(MException), last(MException)
![]() | throw (MException) | tic, toc | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |