Matlab catch exception

Matlab catch exception

When writing Matlab applet, we often encounter some small errors or exceptions. When running the program, I hope to be able to catch these errors and output their error information, and then write the corresponding handler when the error occurs. Here is a summary of Matlab's error capture and optionally output error messages.

1. MException capture exception information class

Matlab provides an MException class to save error messages caught while executing code. When an error is found, Matlab generates an MException class object with the following properties and functions.

Attributes:

    identifier: is a string, each error corresponds to an identifier, which contains at least two substrings separated by semicolons ":", the main form is COMPONENT: MNEMONIC.

    message: is a string, mainly used to feedback error information to the user.

    stack: used to save the stack relationship between calls between each m file

    cause: The Cell Array of MExceptions that caused the error.

function:

    throw(): Output the error message on the command window and terminate the execution of the program.

    rethrow(): Re-output the captured error on the command window and terminate the execution of the program.

    throwAsCaller(): Output the error message on the command window by calling the function.

    addCause(): Superimpose two MException error messages and return a new MException object, which contains the error messages of the superimposed two MException objects.

    getReport(): Get the message property of the error message and return it.

    last(): Returns the latest uncaught error message.

2. The basic form of catching exceptions

Form 1

Pass the captured error directly to an MException object, ErrorInfo, and let Matlab assign values ​​to the properties (identifier, message, etc.) of the ErrorInfo object.

a = [1,2,3];
b = [2,3];
try
    c = [a; b]; % There is an error (the dimensions of the columns are inconsistent), jump to the catch statement line and execute
    disp(c); % After the error is found, this line of code is not executed
catch ErrorInfo % The caught error is an MException object
    disp(ErrorInfo);
    disp(ErrorInfo.identifier);
    disp(ErrorInfo.message);
    disp(ErrorInfo.stack);
    disp(ErrorInfo.cause);
    
    %Other actions when an error occurs

end

Form 2

Directly generate an MException object ME, and manually initialize and assign properties in the ME object.

The initialization format of the MException class object is:

MException(MSGID, ERRMSG, V1, V2, ..., VN), where MSGID is identifier, ERRMSG is message, where ERRMSG can be a format control string containing %s, %d, etc., and V1, V2...VN is the value corresponding to the format control string.

inputstr = input('Type a variable name:', 's');
if ~exist(inputstr, 'var')
   ME = MException('MyComponent:noSuchVariable', 'Variable %s not found', inputstr);% directly generate an MException object and initialize it
   throw(ME);
end

3. Function introduction

3.1 throw()

Output the error message on the command window and terminate the execution of the program. Its output in the command window is the message property of the error object.


3.2 rethrow()

Re-output the caught error on the command window and terminate the execution of the program. Use this function to re-throw the above ErrorInfo once. Note the subtle differences between the two.


3.3 throwAsCaller()

The error message is output on the command window by calling the function. Use this function to output the error message on the command window in the form of calling a function.


3.4 addCause()

Superimpose two MException error messages, and return a new MException object, which contains the error messages of the superimposed two MException objects.

As shown below, cause1_ME indicates a matrix out-of-bounds access error, and cause2_ME indicates a load error when no such file exists. Then by superimposing these two error messages, the specific effect is shown in the following figure.



3.5 getReport ()

Get the message property of the error message and return it.


3.6 last()

Returns the latest uncaught error message. As shown below, ErrorInfo is the previously captured error information, and then recreates a matlab error (out-of-bounds access matrix), and the error is not caught in an MException object by matlab, so you can use this function to return the latest uncaught one error message.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325727034&siteId=291194637