Using the C Math Library | ![]() ![]() |
Customizing Error Handling
Two aspects of the default error handling behavior of the MATLAB C Math Library routines may not suit every application:
You may want control to return to your application when an error occurs, allowing it to perform clean up processing before exiting. You may also prefer to direct error and warning messages to a different output stream than the normal messages output by your application. The following sections describe how to make these customizations.
Continuing Processing After Errors
To return control to your application after a library routine encounters an error condition, you must define try and catch blocks in your application.
When you define a try block, the library warning-level processing does not change. The routines output a warning message and return control back to the application. The default library error processing, however, does change. When you define a try block, the library routines do not output an error message to the user and then exit. Instead, the routines transfer control to your catch block, which performs the error handling processing.
For example, if you want to output an error message to the user, you must do so from your catch block. (You can use the mlfLasterr()
routine to obtain the text of the message associated with the most recent error that occurred.)
Defining a Try Block. A try block is a group of one or more statements, enclosed in braces, introduced by the mlfTry
macro:
mlfTry { /* your code */ }
Note that the mlfTry
macro does not require parentheses; it is not a procedure call.
Defining a Catch Block. A catch block is a group of one or more routines, enclosed in braces, introduced by the mlfCatch
macro and terminated by the mlfEndCatch
macro:
mlfCatch { /* Your code */ } mlfEndCatch
The catch block contains error processing code. For example, you could put clean-up code in your catch block to free allocated storage before exiting. The mlfCatch
and mlfEndCatch
macros do not require parentheses.
![]() | Error Handling Overview | Example - Defining Try/Catch Blocks (ex6.c) | ![]() |