v0.8.23
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 
21 namespace MoFEM {
22 
23 /**
24  * @brief Exceptions and handling errors data structures
25  *
26  */
27 namespace Exceptions {
28 /**
29  * \brief Exception to catch
30  */
31 struct MoFEMException : public std::exception {
32  const int errorCode;
33  char errorMessage[255];
34  MoFEMException(const MoFEMErrorCodes error_code) : errorCode(error_code) {
35  strcpy(errorMessage, "Huston we have a problem, something is wrong");
36  }
37  MoFEMException(const MoFEMErrorCodes error_code, const char error_message[])
38  : errorCode(error_code) {
39  strcpy(errorMessage, error_message);
40  }
41  const char *what() const throw() { return errorMessage; }
42 
43 protected:
44  MoFEMException(const int error_code) : errorCode(error_code) {
45  strcpy(errorMessage, "Huston 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  strcpy(errorMessage, error_message);
62  }
63 };
64 
65 typedef moab::ErrorCode MoABErrorCode; ///< MoAB error code
66 typedef PetscErrorCode MoFEMErrorCode; ///< MoFEM/PETSc error code
67 
68 template <typename TYPE> struct MoFEMErrorCodeGeneric {
69  MoFEMErrorCodeGeneric(const TYPE) {}
70 };
71 
72 template <> struct MoFEMErrorCodeGeneric<PetscErrorCode> {
73  PetscErrorCode iERR;
74  MoFEMErrorCodeGeneric(const PetscErrorCode ierr) : iERR(ierr) {}
75  inline operator PetscErrorCode() const { return iERR; }
76 };
77 
78 template <> struct MoFEMErrorCodeGeneric<moab::ErrorCode> {
79  moab::ErrorCode rVAL;
80  MoFEMErrorCodeGeneric(const moab::ErrorCode rval) : rVAL(rval) {}
81  inline operator moab::ErrorCode() const { return rVAL; }
82 };
83 
88 
89 /**
90  * \brief Error check for inline function check.
91  *
92  * This class is not used directly, it is called in CHKERR. In case of the error
93  * pass line number and that is catch at the end of the function. Information is
94  * enriched by function name and file name. Then error is pushed to PETSc error
95  * stack.
96  *
97  * \note This class has no variables and line number is set at compilation.
98  * Adding variables to this function will reduce efficiency of the code. Do
99  * not do that.
100  *
101  */
102 template <int LINE> struct ErrorCheckerCode {
103 
104  /**
105  * @brief Operator for handling PetscErrorCode and MoFEMErrorCode
106  *
107  */
108  inline void operator<<(const MoFEMErrorCode err) {
109  if (PetscUnlikely(err)) {
110  throw MoFEMExceptionRepeat(err, LINE);
111  }
112  return;
113  }
114 
115  /**
116  * @brief Operator for handling moab::ErrorCode
117  *
118  */
119  inline void operator<<(const moab::ErrorCode err) {
120  if (PetscLikely(MB_SUCCESS != err)) {
121  std::string error_str = (unsigned)err <= (unsigned)MB_FAILURE
122  ? moab::ErrorCodeStr[err]
123  : "INVALID ERROR CODE";
124  std::string str("MOAB error (" + boost::lexical_cast<std::string>(err) +
125  ") " + error_str);
126  throw MoFEMExceptionInitial(MOFEM_MOAB_ERROR, str.c_str(), LINE);
127  }
128  return;
129  }
130 };
131 
132 } // namespace Exceptions
133 
134 using namespace Exceptions;
135 
136 } // namespace MoFEM
137 
138 #endif // __EXCEPTIONS_HPP__
moab::ErrorCode MoABErrorCode
MoAB error code.
Definition: Exceptions.hpp:65
void operator<<(const MoFEMErrorCode err)
Operator for handling PetscErrorCode and MoFEMErrorCode.
Definition: Exceptions.hpp:108
MoFEMExceptionRepeat(const int error_code, const int line)
Definition: Exceptions.hpp:51
MoFEMException(const MoFEMErrorCodes error_code, const char error_message[])
Definition: Exceptions.hpp:37
Error check for inline function check.
Definition: Exceptions.hpp:102
void operator<<(const moab::ErrorCode err)
Operator for handling moab::ErrorCode.
Definition: Exceptions.hpp:119
implementation of Data Operators for Forces and Sources
Definition: Common.hpp:21
static MoFEMErrorCodeGeneric< moab::ErrorCode > rval
Definition: Exceptions.hpp:84
static MoFEMErrorCodeGeneric< PetscErrorCode > ierr
Definition: Exceptions.hpp:86
MoFEMException(const MoFEMErrorCodes error_code)
Definition: Exceptions.hpp:34
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
Definition: Exceptions.hpp:66
MoFEMException(const int error_code)
Definition: Exceptions.hpp:44
MoFEMErrorCodes
Error handling.
Definition: definitions.h:113
MoFEMExceptionInitial(const int error_code, const char error_message[], const int line)
Definition: Exceptions.hpp:58