Exception Handling

Cantera throws exceptions of type CanteraError when an error is encountered.

Your program should always catch these. You can throw CanteraError exceptions too, if you like, or you can use your own application-specific exception classes.

#include <cantera/Cantera.h>
//
// artifical example of throwing and catching a CanteraError exception.
//
void mycode() {
    ThermoPhase* gas = newPhase("h2o2.cti","ohmech");
    if (gas->temperature() < 3000.0) {
        throw CanteraError("mycode","test of exception throwing and catching");
    }
}

int main() {
    try {
        mycode();
    }
    catch (CanteraError) {
        showErrors();
        error("program terminating.");
    }
}

The function showErrors is a convenient way to display the error message in the catch block. The error function prints an error and terminates execution. Note that both of these functions are environment-specific; i.e. they behave differently if you are running your Cantera code embedded in a MATLAB application than if you run it as a stand-alone C++ application.

See also:
Writing messages to the screen

The output generated when this program is run is shown below.

************************************************
                   Cantera Error!                  
************************************************


Procedure: mycode
Error:     test of exception throwing and catching


program terminating.
Generated by  doxygen 1.6.3