Using the C++ Math Library    

Handling Exceptions

The MATLAB C++ Math Library delivers error messages via exceptions. This section:

Refer to Appendix C for a list of MATLAB C++ error messages.

C++ Exception Handling Overview

Many earlier error-handling schemes reported errors via a return value from a function. That mechanism was inconvenient and unreliable for two reasons. First, it did not allow function composition, where one function call is nested in the argument list of another. For example, f(g(x)) composes f() and g(). Second, the scheme placed the burden for checking error codes on the programmer.

Many other schemes, including the one used by the standard C library, use a global variable in place of returned error codes. This mechanism solves one problem, function composition, but still requires that the programmer check for errors.

The C++ exception-handling mechanism suffers from neither of these problems. Exception-handling does not require that each function return an error code, which means that functions can be composed. In addition, exceptions cannot be ignored by a programmer because an uncaught exception terminates the program. If a programmer forgets to handle an exception, abrupt program termination is a potent reminder.


 Output to a GUI Handling C++ Math Library Exceptions in Your Code