Rethrow previously caught exception
a previously caught exception,
exception. MATLAB® typically
responds to errors by terminating the currently running program. However,
you can use a
try/catch block to catch the exception.
This interrupts the program termination so you can execute your own
error handling procedures. End the
rethrow statement to terminate the program
and redisplay the exception.
rethrow handles the stack trace differently
Instead of creating the stack from where MATLAB executes the
rethrow preserves the original exception
information and enables you to retrace the source of the original
exception— Exception containing cause and location of error
Exception containing the cause and location of an error, specified
as a scalar
Cause MATLAB to throw an error by calling
no inputs. Catch the exception, display the error identifier, and
rethrow the exception.
try surf catch ME disp(['ID: ' ME.identifier]) rethrow(ME) end
ID: MATLAB:narginchk:notEnoughInputs Error using surf (line 49) Not enough input arguments.
Create a function,
your working folder.
function C = combineArrays(A,B) try C = catAlongDim1(A,B); % Line 3 catch exception throw(exception) % Line 5 end end function V = catAlongDim1(V1,V2) V = cat(1,V1,V2); % Line 10 end
combineArrays function with
arrays of different sizes.
A = 1:5; B = 1:4; combineArrays(A,B)
Error using combineArrays (line 5) Dimensions of matrices being concatenated are not consistent.
The stack refers to line 5 where MATLAB throws the exception.
line 5 of the
combineArrays function, and call
the function again.
Error using cat Dimensions of matrices being concatenated are not consistent. Error in combineArrays>catAlongDim1 (line 10) V = cat(1,V1,V2); % Line 10 Error in combineArrays (line 3) C = catAlongDim1(A,B); % Line 3
rethrow method maintains the original stack
and indicates the error is on line 3.