MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
end

ans =
file: 'C:\matlab\test\mfileB.m'
name: 'B2'
line: 31
ans =
file: 'C:\matlab\test\mfileB.m'
name: 'B1'
line: 9
ans =
file: 'C:\matlab\test\mfileA.m'
name: 'A1'
line: 43

The Cause Array

In some situations, it can be important to record information about not only the one
command that caused execution to stop, but also other exceptions that your code caught.
You can save these additional MException objects in the cause field of the primary
exception.

The cause field of an MException is an optional cell array of related MException
objects. You must use the following syntax when adding objects to the cause cell array:
primaryException = addCause(primaryException, secondaryException)

This example attempts to assign an array D to variable X. If the D array does not exist, the
code attempts to load it from a MAT-file and then retries assigning it to X. If the load fails,
a new MException object (ME3) is constructed to store the cause of the first two errors
(ME1 and ME2):

try
X = D(1:25)
catch ME1
try
filename = 'test200';
load(filename);
X = D(1:25)
catch ME2
ME3 = MException('MATLAB:LoadErr', ...
'Unable to load from file %s', filename);
ME3 = addCause(ME3, ME1);
ME3 = addCause(ME3, ME2);

26 Error Handling

Free download pdf