Cantera  2.1.2
Classes | Macros | Functions
Error Handling

These classes and related functions are used to handle errors and unknown events within Cantera. More...

Classes

class  CanteraError
 Base class for exceptions thrown by Cantera classes. More...
 
class  ArraySizeError
 Array size error. More...
 
class  IndexError
 An array index is out of range. More...
 

Macros

#define AssertTrace(expr)   ((expr) ? (void) 0 : throw Cantera::CanteraError(STR_TRACE, std::string("failed assert: ") + #expr))
 Assertion must be true or an error is thrown. More...
 
#define AssertThrow(expr, procedure)   ((expr) ? (void) 0 : throw Cantera::CanteraError(procedure, std::string("failed assert: ") + #expr))
 Assertion must be true or an error is thrown. More...
 
#define AssertThrowMsg(expr, procedure, message)   ((expr) ? (void) 0 : throw Cantera::CanteraError(procedure + std::string(": at failed assert: \"") + std::string(#expr) + std::string("\""), message))
 Assertion must be true or an error is thrown. More...
 

Functions

void addError (const std::string &r, const std::string &msg)
 Set an error condition in the application class without throwing an exception. More...
 
int getErrorCount ()
 Return the number of errors that have been encountered so far. More...
 
void popError ()
 Discard the last error message. More...
 
std::string lastErrorMessage ()
 Retrieve the last error message in a string. More...
 
void getErrors (std::ostream &f)
 Prints all of the error messages to an ostream. More...
 
void logErrors ()
 Prints all of the error messages using writelog. More...
 
int nErrors ()
 Return the number of errors that have been encountered so far. More...
 

Detailed Description

These classes and related functions are used to handle errors and unknown events within Cantera.

The general idea is that exceptions are thrown using the common base class called CanteraError. Derived types of CanteraError characterize what type of error is thrown. A list of all of the thrown errors is kept in the Application class.

Any exceptions which are not caught cause a fatal error exit from the program.

Below is an example of how to catch errors that throw the CanteraError class. In general, all Cantera C++ programs will have this basic structure.

#include "cantera/thermo.h"
using namespace Cantera;
// The actual code is put into a function that
// can be called from the main program.
void simple_demo()
{
// Create a new phase
ThermoPhase* gas = newPhase("h2o2.cti","ohmech");
// Set its state by specifying T (500 K) P (2 atm) and the mole
// fractions. Note that the mole fractions do not need to sum to
// 1.0 - they will be normalized internally. Also, the values for
// any unspecified species will be set to zero.
gas->setState_TPX(500.0, 2.0*OneAtm, "H2O:1.0, H2:8.0, AR:1.0");
// Print a summary report of the state of the gas
std::cout << gas->report() << std::endl;
// Clean up
delete gas;
}
// the main program just calls function simple_demo within
// a 'try' block, and catches CanteraError exceptions that
// might be thrown
int main()
{
try {
simple_demo();
} catch (CanteraError& err) {
std::cout << err.what() << std::endl;
}
}

The function showErrors() will print out the fatal error condition to standard output.

A group of defines may be used during debugging to assert conditions which should be true. These are named AssertTrace(), AssertThrow(), and AssertThrowMsg(). Examples of their usage is given below.

AssertThrow(p == OneAtm, "Kinetics::update");
AssertThrowMsg(p == OneAtm, "Kinetics::update",
"Algorithm limited to atmospheric pressure");

Their first argument is a boolean. If the boolean is not true, a CanteraError is thrown, with descriptive information indicating where the error occurred. The Assert* checks are skipped if the NDEBUG preprocessor symbol is defined, e.g. with the compiler option -DNDEBUG.

Macro Definition Documentation

#define AssertTrace (   expr)    ((expr) ? (void) 0 : throw Cantera::CanteraError(STR_TRACE, std::string("failed assert: ") + #expr))

Assertion must be true or an error is thrown.

Assertion must be true or else a CanteraError is thrown. A diagnostic string containing the file and line number, indicating where the error occurred is added to the thrown object.

Parameters
exprBoolean expression that must be true

Definition at line 216 of file ctexceptions.h.

Referenced by HMWSoln::applyphScale(), IonsFromNeutralVPSSTP::calcNeutralMoleculeMoleFractions(), Phase::entropyElement298(), Elements::entropyElement298(), NasaThermo::fixDiscontinuities(), IonsFromNeutralVPSSTP::getNeutralMoleculeMoleGrads(), Cantera::importPhase(), VPStandardStateTP::initThermoXML(), WaterTransport::initTP(), rxninfo::installReaction(), HMWSoln::s_updateScaling_pHScaling(), HMWSoln::s_updateScaling_pHScaling_dP(), HMWSoln::s_updateScaling_pHScaling_dT(), and HMWSoln::s_updateScaling_pHScaling_dT2().

#define AssertThrow (   expr,
  procedure 
)    ((expr) ? (void) 0 : throw Cantera::CanteraError(procedure, std::string("failed assert: ") + #expr))

Assertion must be true or an error is thrown.

Assertion must be true or else a CanteraError is thrown. A diagnostic string indicating where the error occurred is added to the thrown object.

Parameters
exprBoolean expression that must be true
procedureCharacter string or std:string expression indicating the procedure where the assertion failed

Definition at line 229 of file ctexceptions.h.

Referenced by vcs_VolPhase::elemGlobalIndex(), MolalityVPSSTP::getActivityCoefficients(), IdealMolalSoln::getChemPotentials(), SimpleTransport::getSpeciesFluxesExt(), STITbyPDSS::initAllPtrs(), PDSS::initPtrs(), PDSS::initThermo(), PDSS::initThermoXML(), GeneralSpeciesThermo::install_STIT(), vcs_VolPhase::setElemGlobalIndex(), and RootFind::solve().

#define AssertThrowMsg (   expr,
  procedure,
  message 
)    ((expr) ? (void) 0 : throw Cantera::CanteraError(procedure + std::string(": at failed assert: \"") + std::string(#expr) + std::string("\""), message))

Assertion must be true or an error is thrown.

Assertion must be true or else a CanteraError is thrown. A diagnostic string indicating where the error occurred is added to the thrown object.

Parameters
exprBoolean expression that must be true
procedureCharacter string or std:string expression indicating the procedure where the assertion failed
messageCharacter string or std:string expression containing a descriptive message is added to the thrown error condition.

Definition at line 247 of file ctexceptions.h.

Referenced by VPStandardStateTP::_updateStandardStateThermo(), Application::Messages::endLogGroup(), Phase::entropyElement298(), Elements::entropyElement298(), NasaThermo::install(), MolalityVPSSTP::setSolvent(), Sim1D::setValue(), STITbyPDSS::updatePropertiesTemp(), and Sim1D::value().

Function Documentation

void addError ( const std::string &  r,
const std::string &  msg 
)

Set an error condition in the application class without throwing an exception.

This routine adds an error message to the end of the stack of errors that Cantera accumulates in the Application class.

Parameters
rProcedure name which is generating the error condition
msgDescriptive message of the error condition.

Definition at line 115 of file application.cpp.

Referenced by Application::addError().

int getErrorCount ( )

Return the number of errors that have been encountered so far.

Definition at line 121 of file application.cpp.

Referenced by Application::getErrorCount().

void popError ( )

Discard the last error message.

Cantera saves a stack of exceptions that it has caught in the Application class. This routine eliminates the last exception to be added to that stack.

Definition at line 531 of file application.cpp.

References Application::Messages::errorMessage, and Application::Messages::errorRoutine.

Referenced by Application::popError().

std::string lastErrorMessage ( )

Retrieve the last error message in a string.

This routine will retrieve the last error message and return it in the return string.

Definition at line 539 of file application.cpp.

Referenced by Application::lastErrorMessage().

void getErrors ( std::ostream &  f)

Prints all of the error messages to an ostream.

Write out all of the saved error messages to the ostream f using the function Logger::writelog. Cantera saves a stack of exceptions that it has caught in the Application class. This routine writes out all of the error messages to the ostream and then clears them from internal storage.

Parameters
fostream which will receive the error messages

Definition at line 553 of file application.cpp.

Referenced by Application::getErrors().

void logErrors ( )

Prints all of the error messages using writelog.

Print all of the error messages using function writelog. Cantera saves a stack of exceptions that it has caught in the Application class. This routine writes out all of the error messages and then clears them from internal storage.

Definition at line 575 of file application.cpp.

References Application::writelog().

Referenced by Application::logErrors().

int nErrors ( )

Return the number of errors that have been encountered so far.

Definition at line 156 of file global.cpp.

References Cantera::app(), and Application::getErrorCount().