addCause
Attaches an underlying exception to an MException as one of its causes.
Category: Programming > Programming Utilities
Syntax
baseME = addCause(baseME, cause)
Returns a copy of baseME whose cause list also holds the exception cause.
Description
Errors often have a chain of reasons: a report could not be built because a file could not be read, which failed because a folder was missing. addCause records each lower-level exception in the cause property of the top-level one, a cell array, so the full chain travels together when the exception is thrown and can be inspected after it is caught.
Input arguments
baseME- The high-level
MExceptionthat describes the failure. cause- An
MExceptiondescribing a lower-level reason.
Output arguments
baseME- The updated exception. Assign it back; the original object is not modified.
Example
Recording a cause
ME = MException('app:load', 'Could not load data');
cause = MException('io:missing', 'File not found');
ME = addCause(ME, cause);
disp(numel(ME.cause))
disp(ME.cause{1}.message)
Output
1
File not found