MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

The Call Stack


The stack field of the MException object identifies the line number, function, and
filename where the error was detected. If the error occurs in a called function, as in the
following example, the stack field contains the line number, function name, and filename
not only for the location of the immediate error, but also for each of the calling functions.
In this case, stack is an N-by-1 array, where N represents the depth of the call stack.
That is, the stack field displays the function name and line number where the exception
occurred, the name and line number of the caller, the caller's caller, etc., until the top-
most function is reached.


When throwing an exception, MATLAB stores call stack information in the stack field.
You cannot write to this field; access is read-only.


For example, suppose you have three functions that reside in two separate files:


mfileA.m


.
.
42 function A1(x, y)
43 B1(x, y);


mfileB.m


.
.
8 function B1(x, y)
9 B2(x, y)
.
.
26 function B2(x, y)
27.
28.
29.
30.
31 % Throw exception here


Catch the exception in variable ME and then examine the stack field:


for k=1:length(ME.stack)
ME.stack(k)


Capture Information About Exceptions
Free download pdf