Using the C Math Library    

Replacing the Default Library Error Handler 

The default error handling behavior of the MATLAB C Math Library routines is implemented by the default library error handler routine. You can further customize error handling by replacing the default library error handler routine with one of your own design.

To replace the default error handler you must:

Writing an Error Handler

When you write an error handler, you must conform to the library prototype for error handling routines:

In this prototype, note the following:

Registering Your Error Handler

After writing an error handler, you must register it with the MATLAB C Math Library so that the library routines can call it when they encounter an error condition at runtime. You register an error handler using the mlfSetErrorHandler() routine.

Example - Adding an Error Handler

The following example program adds an error handler to the sample program introduced on page 8-3. This error handler writes error messages to a log file, identifying each message as an error or warning. An error handler like this allows a program to run unattended, since any errors produced are recorded in a file for future examination. More complex error-handling schemes are also possible. For example, you can use two files, one for the messages sent to the print handler and one for errors, or you can pipe the error message to an e-mail program that sends a notification of the error to the user who started the program.

The numbered items in the list below correspond to the numbered comments in the code example:

  1. The example program declares a variable, stream, that is a pointer to the output log file.
  2. The error handling routine, MyErrorHandler, determines the type of error message, and writes warnings and errors to a log file.
  3. The main program opens the log file, named myerrlog.out. The program calls fclose() to close the log file before exiting.
  4. Register the error handler with the MATLAB C Math Library with this call to mlfSetErrorHandler().

    Output

The program produces this output.


 Example - Defining Try/Catch Blocks (ex6.c) Defining a Print Handler