Programming and Data Types    

Handling and Recovering from an Error

The catch segment of a try-catch block needs to effectively handle any errors that may be caught by the preceding try. Frequently, you will want to simply report the error and stop execution. This prevents erroneous data from being propagated into the remainder of the program.

Reporting an Error

To report an error and halt program execution, use the MATLAB error function. You determine what the error mesage will be by specifying it as an input to the error function in your code. For example,

displays the message shown below when n is equal to zero.

Formatted Message Strings.   The error message string that you specify can also contain formatting conversion characters, such as those used with the MATLAB sprintf function. Make the error string the first argument, and then add any variables used by the conversion as subsequent arguments.

For example, if your program cannot find a specific file, you might report the error with

Message Identifiers.   Use a message identifier argument with error to attach a unique tag to that error message. MATLAB uses this tag to better identify the source of an error. The first argument in this example is the message identifier.

See Using Message Identifiers with lasterr for more information on how to use identifiers with errors.

Formatted String Conversion.   MATLAB converts special characters (like \n and %d) in the error message string only when you specify more than one input argument with error. In the single argument case shown below, \n is taken to mean backslash-n. It is not converted to a newline character.

But, when more than one argument is specified, MATLAB does convert special characters. This is true regardless of whether the additional argument supplies conversion values or is a message identifier.

Identifying an Error

Once an error has been caught, you will need to know the source of the error in order to handle it appropriately. The lasterr function returns information that enables you to identify the error that was most recently generated by MATLAB.

To return the most recent error message to the variable errormsg, type

You can also change the text of the last error message with a new message or with an empty string as shown below. You might want to do this if a lower level routine detects an error that you don't want visible to the upper levels.

Example.   The matrix_multiply function shown earlier in this section could fail for various reasons. If it is called with incompatible matrices, for example, lasterr returns the following string.

This example uses lasterr to determine the cause of an error in matrix_multiply.

When calling the function with two matrices not compatible for matrix multiplication, you get the following error message.

When calling the function with a cell array argument, you get a message that addresses that error.

Regenerating an Error

Use the rethrow function to regenerate an error that has previously been thrown. You may want to do this from the catch part of a try-catch block, for example, after performing some required cleanup tasks following an error.

This is how you would rethrow an error in a try-catch block:

rethrow generates an error based on the err input argument that you provide. This argument must be a MATLAB structure with at least the following two fields.

Fieldname
Description
message
Text of the error message
identifier
Message identifier of the error message

If you simply want to regenerate the last error that occurred, the lasterror function returns a structure that can then be passed directly to rethrow.


  Errors and Warnings Warnings