v0.14.0
Loading...
Searching...
No Matches
Exceptions.hpp
Go to the documentation of this file.
1/** \file Exceptions.hpp
2 * \brief Exceptions and error handlers
3 */
4
5
6
7#ifndef __EXCEPTIONS_HPP__
8#define __EXCEPTIONS_HPP__
9
10namespace MoFEM {
11
12/**
13 * @brief Exceptions and handling errors data structures
14 *
15 */
16namespace Exceptions {
17/**
18 * \brief Exception to catch
19 */
20struct MoFEMException : public std::exception {
21 const int errorCode;
22 char errorMessage[1024];
24 : MoFEMException(static_cast<int>(error_code)) {}
25 MoFEMException(const MoFEMErrorCodes error_code, const char error_message[])
26 : errorCode(error_code) {
27 strncpy(errorMessage, error_message, sizeof(errorMessage));
28 errorMessage[sizeof(errorMessage) - 1] = '\0';
29 }
30 const char *what() const throw() { return errorMessage; }
31
32protected:
33 MoFEMException(const int error_code) : errorCode(error_code) {
34 strcpy(errorMessage, "Houston we have a problem, something is wrong");
35 }
36};
37
39 const int lINE;
40 MoFEMExceptionRepeat(const int error_code, const int line)
41 : MoFEMException(error_code), lINE(line) {
42 strcpy(errorMessage, " ");
43 }
44};
45
47 MoFEMExceptionInitial(const int error_code, const char error_message[],
48 const int line)
49 : MoFEMExceptionRepeat(error_code, line) {
50 strncpy(errorMessage, error_message, sizeof(errorMessage));
51 errorMessage[sizeof(errorMessage) - 1] = '\0';
52 }
53};
54
55typedef moab::ErrorCode MoABErrorCode; ///< MoAB error code
56typedef PetscErrorCode MoFEMErrorCode; ///< MoFEM/PETSc error code
57
58template <typename TYPE> struct MoFEMErrorCodeGeneric {
59 MoFEMErrorCodeGeneric(const TYPE) {}
60};
61
62template <> struct MoFEMErrorCodeGeneric<PetscErrorCode> {
63 PetscErrorCode iERR;
64 MoFEMErrorCodeGeneric(const PetscErrorCode ierr) : iERR(ierr) {}
65 inline operator PetscErrorCode() const { return iERR; }
66};
67
68template <> struct MoFEMErrorCodeGeneric<moab::ErrorCode> {
69 moab::ErrorCode rVAL;
70 MoFEMErrorCodeGeneric(const moab::ErrorCode rval) : rVAL(rval) {}
71 inline operator moab::ErrorCode() const { return rVAL; }
72};
73
78
79/**
80 * \brief Error check for inline function check.
81 *
82 * This class is not used directly, it is called in CHKERR. In case of the error
83 * pass line number and that is catch at the end of the function. Information is
84 * enriched by function name and file name. Then error is pushed to PETSc error
85 * stack.
86 *
87 * \note This class has no variables and line number is set at compilation.
88 * Adding variables to this function will reduce efficiency of the code. Do
89 * not do that.
90 *
91 */
92template <int LINE> struct ErrorChecker {
93
94 /**
95 * @brief Operator for handling PetscErrorCode and MoFEMErrorCode
96 *
97 */
98 inline void operator<<(const MoFEMErrorCode err) {
99 if (PetscUnlikely(err)) {
100 throw MoFEMExceptionRepeat(err, LINE);
101 }
102 return;
103 }
104
105 /**
106 * @brief Operator for handling moab::ErrorCode
107 *
108 */
109 inline void operator<<(const moab::ErrorCode err) {
110 if (PetscLikely(MB_SUCCESS != err)) {
111 std::string error_str = (unsigned)err <= (unsigned)MB_FAILURE
112 ? moab::ErrorCodeStr[err]
113 : "INVALID ERROR CODE";
114 std::string str("MOAB error (" + boost::lexical_cast<std::string>(err) +
115 ") " + error_str);
116 throw MoFEMExceptionInitial(MOFEM_MOAB_ERROR, str.c_str(), LINE);
117 }
118 return;
119 }
120};
121
122} // namespace Exceptions
123
124using namespace Exceptions;
125
126} // namespace MoFEM
127
128#endif // __EXCEPTIONS_HPP__
MoFEMErrorCodes
Error handling.
Definition definitions.h:29
@ MOFEM_MOAB_ERROR
Definition definitions.h:41
moab::ErrorCode MoABErrorCode
MoAB error code.
static MoFEMErrorCodeGeneric< PetscErrorCode > ierr
static MoFEMErrorCodeGeneric< moab::ErrorCode > rval
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
implementation of Data Operators for Forces and Sources
Definition Common.hpp:10
Error check for inline function check.
void operator<<(const moab::ErrorCode err)
Operator for handling moab::ErrorCode.
void operator<<(const MoFEMErrorCode err)
Operator for handling PetscErrorCode and MoFEMErrorCode.
MoFEMException(const int error_code)
MoFEMException(const MoFEMErrorCodes error_code)
MoFEMException(const MoFEMErrorCodes error_code, const char error_message[])
MoFEMExceptionInitial(const int error_code, const char error_message[], const int line)
MoFEMExceptionRepeat(const int error_code, const int line)