Cantera  3.2.0a2
Loading...
Searching...
No Matches
clib_utils.h
Go to the documentation of this file.
1/**
2 * @file clib_utils.h
3 */
4
5// This file is part of Cantera. See License.txt in the top-level directory or
6// at https://cantera.org/license.txt for license and copyright information.
7
8#ifndef __CLIB_UTILS_H__
9#define __CLIB_UTILS_H__
10
13
14#include "application.h"
15#include "../clib/Cabinet.h" // File needs to be moved
16#include <iostream>
17
18
19namespace Cantera
20{
21
22//! Exception handler used at language interface boundaries.
23/*!
24 * When called from a "catch (...)" block, this function will attempt to save
25 * an error message in global error stack and return a value indicating the
26 * type of exception caught.
27 *
28 * @param ctErrorCode Value to return if a CanteraError is caught
29 * @param otherErrorCode Value to return if a different exception is caught
30 */
31template <typename T>
32T handleAllExceptions(T ctErrorCode, T otherErrorCode)
33{
34 // Rethrow the previous exception, then catch a more
35 // specific exception type if possible.
36 try {
37 throw;
38 } catch (CanteraError& cterr) {
40 return ctErrorCode;
41 } catch (std::exception& err) {
42 std::cerr << "Cantera: caught an instance of "
43 << err.what() << std::endl;
44 Application::Instance()->addError(err.what());
45 return otherErrorCode;
46 } catch (...) {
47 std::cerr << "Cantera: caught an instance of "
48 "an unknown exception type" << std::endl;
49 Application::Instance()->addError("unknown C++ exception");
50 return otherErrorCode;
51 }
52}
53
54}
55
56#endif
static Application * Instance()
Return a pointer to the one and only instance of class Application.
void addError(const string &r, const string &msg="")
Set an error condition in the application class without throwing an exception.
Base class for exceptions thrown by Cantera classes.
const char * what() const override
Get a description of the error.
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595
T handleAllExceptions(T ctErrorCode, T otherErrorCode)
Exception handler used at language interface boundaries.
Definition clib_utils.h:32