v0.13.1
Exceptions.hpp
Go to the documentation of this file.
1/** \file Exceptions.hpp
2 * \brief Exceptions and error handlers
3 *
4 * MoFEM is free software: you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License as published by the
6 * Free Software Foundation, either version 3 of the License, or (at your
7 * option) any later version.
8 *
9 * MoFEM is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
12 * License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with MoFEM. If not, see <http://www.gnu.org/licenses/>
16 */
17
18#ifndef __EXCEPTIONS_HPP__
19#define __EXCEPTIONS_HPP__
20
21namespace MoFEM {
22
23/**
24 * @brief Exceptions and handling errors data structures
25 *
26 */
27namespace Exceptions {
28/**
29 * \brief Exception to catch
30 */
31struct MoFEMException : public std::exception {
32 const int errorCode;
33 char errorMessage[1024];
35 : MoFEMException(static_cast<int>(error_code)) {}
36 MoFEMException(const MoFEMErrorCodes error_code, const char error_message[])
37 : errorCode(error_code) {
38 strncpy(errorMessage, error_message, sizeof(errorMessage));
39 errorMessage[sizeof(errorMessage) - 1] = '\0';
40 }
41 const char *what() const throw() { return errorMessage; }
42
43protected:
44 MoFEMException(const int error_code) : errorCode(error_code) {
45 strcpy(errorMessage, "Houston we have a problem, something is wrong");
46 }
47};
48
50 const int lINE;
51 MoFEMExceptionRepeat(const int error_code, const int line)
52 : MoFEMException(error_code), lINE(line) {
53 strcpy(errorMessage, " ");
54 }
55};
56
58 MoFEMExceptionInitial(const int error_code, const char error_message[],
59 const int line)
60 : MoFEMExceptionRepeat(error_code, line) {
61 strncpy(errorMessage, error_message, sizeof(errorMessage));
62 errorMessage[sizeof(errorMessage) - 1] = '\0';
63 }
64};
65
66typedef moab::ErrorCode MoABErrorCode; ///< MoAB error code
67typedef PetscErrorCode MoFEMErrorCode; ///< MoFEM/PETSc error code
68
69template <typename TYPE> struct MoFEMErrorCodeGeneric {
70 MoFEMErrorCodeGeneric(const TYPE) {}
71};
72
73template <> struct MoFEMErrorCodeGeneric<PetscErrorCode> {
74 PetscErrorCode iERR;
75 MoFEMErrorCodeGeneric(const PetscErrorCode ierr) : iERR(ierr) {}
76 inline operator PetscErrorCode() const { return iERR; }
77};
78
79template <> struct MoFEMErrorCodeGeneric<moab::ErrorCode> {
80 moab::ErrorCode rVAL;
81 MoFEMErrorCodeGeneric(const moab::ErrorCode rval) : rVAL(rval) {}
82 inline operator moab::ErrorCode() const { return rVAL; }
83};
84
89
90/**
91 * \brief Error check for inline function check.
92 *
93 * This class is not used directly, it is called in CHKERR. In case of the error
94 * pass line number and that is catch at the end of the function. Information is
95 * enriched by function name and file name. Then error is pushed to PETSc error
96 * stack.
97 *
98 * \note This class has no variables and line number is set at compilation.
99 * Adding variables to this function will reduce efficiency of the code. Do
100 * not do that.
101 *
102 */
103template <int LINE> struct ErrorChecker {
104
105 /**
106 * @brief Operator for handling PetscErrorCode and MoFEMErrorCode
107 *
108 */
109 inline void operator<<(const MoFEMErrorCode err) {
110 if (PetscUnlikely(err)) {
111 throw MoFEMExceptionRepeat(err, LINE);
112 }
113 return;
114 }
115
116 /**
117 * @brief Operator for handling moab::ErrorCode
118 *
119 */
120 inline void operator<<(const moab::ErrorCode err) {
121 if (PetscLikely(MB_SUCCESS != err)) {
122 std::string error_str = (unsigned)err <= (unsigned)MB_FAILURE
123 ? moab::ErrorCodeStr[err]
124 : "INVALID ERROR CODE";
125 std::string str("MOAB error (" + boost::lexical_cast<std::string>(err) +
126 ") " + error_str);
127 throw MoFEMExceptionInitial(MOFEM_MOAB_ERROR, str.c_str(), LINE);
128 }
129 return;
130 }
131};
132
133} // namespace Exceptions
134
135using namespace Exceptions;
136
137} // namespace MoFEM
138
139#endif // __EXCEPTIONS_HPP__
MoFEMErrorCodes
Error handling.
Definition: definitions.h:42
@ MOFEM_MOAB_ERROR
Definition: definitions.h:54
moab::ErrorCode MoABErrorCode
MoAB error code.
Definition: Exceptions.hpp:66
static MoFEMErrorCodeGeneric< PetscErrorCode > ierr
Definition: Exceptions.hpp:87
static MoFEMErrorCodeGeneric< moab::ErrorCode > rval
Definition: Exceptions.hpp:85
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
Definition: Exceptions.hpp:67
implementation of Data Operators for Forces and Sources
Definition: Common.hpp:21
Error check for inline function check.
Definition: Exceptions.hpp:103
void operator<<(const moab::ErrorCode err)
Operator for handling moab::ErrorCode.
Definition: Exceptions.hpp:120
void operator<<(const MoFEMErrorCode err)
Operator for handling PetscErrorCode and MoFEMErrorCode.
Definition: Exceptions.hpp:109
MoFEMException(const int error_code)
Definition: Exceptions.hpp:44
MoFEMException(const MoFEMErrorCodes error_code)
Definition: Exceptions.hpp:34
MoFEMException(const MoFEMErrorCodes error_code, const char error_message[])
Definition: Exceptions.hpp:36
MoFEMExceptionInitial(const int error_code, const char error_message[], const int line)
Definition: Exceptions.hpp:58
MoFEMExceptionRepeat(const int error_code, const int line)
Definition: Exceptions.hpp:51