Using the C++ Math Library | ![]() ![]() |
Handling C++ Math Library Exceptions in Your Code
Your programs must catch exceptions thrown by the MATLAB C++ Math Library. Uncaught exceptions cause abnormal program termination.
To handle these exceptions, you need to catch each exception and display the message associated with it. C++ provides the mechanism for catching exceptions: the try
and catch
keywords.
Here's a basic example that demonstrates these techniques.
// try-block try { eig(A); } //catch-block catch(mwException &ex) { mwDisplayException(ex); }
The try
keyword introduces a try block. Any exception thrown while executing the code in the try block transfers control to the first catch block that applies to the type of the exception being caught. Each catch block catches one type of exception. You use multiple catch blocks to catch exceptions of different types.
This catch block catches any exception objects derived from the class mwException
. mwException
is the superclass for all exceptions thrown by the MATLAB C++ Math Library. If you catch and display exceptions of this type, you see all the error messages associated with the exceptions thrown by the library. For a complete list of the exceptions defined by the MATLAB C++ Math Library, see Appendix B.
![]() | Handling Exceptions | Example Program: Handling Exceptions (ex5.cpp) | ![]() |