v0.14.0
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 
10 namespace MoFEM {
11 
12 /**
13  * @brief Exceptions and handling errors data structures
14  *
15  */
16 namespace Exceptions {
17 /**
18  * \brief Exception to catch
19  */
20 struct MoFEMException : public std::exception {
21  const int errorCode;
22  char errorMessage[1024];
23  MoFEMException(const MoFEMErrorCodes error_code)
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 
32 protected:
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 
55 typedef moab::ErrorCode MoABErrorCode; ///< MoAB error code
56 typedef PetscErrorCode MoFEMErrorCode; ///< MoFEM/PETSc error code
57 
58 template <typename TYPE> struct MoFEMErrorCodeGeneric {
59  MoFEMErrorCodeGeneric(const TYPE) {}
60 };
61 
62 template <> struct MoFEMErrorCodeGeneric<PetscErrorCode> {
63  PetscErrorCode iERR;
64  MoFEMErrorCodeGeneric(const PetscErrorCode ierr) : iERR(ierr) {}
65  inline operator PetscErrorCode() const { return iERR; }
66 };
67 
68 template <> 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  */
92 template <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 
124 using namespace Exceptions;
125 
126 } // namespace MoFEM
127 
128 #endif // __EXCEPTIONS_HPP__
MoFEM::Exceptions::MoFEMExceptionRepeat
Definition: Exceptions.hpp:38
MoFEM::Exceptions::ErrorChecker::operator<<
void operator<<(const MoFEMErrorCode err)
Operator for handling PetscErrorCode and MoFEMErrorCode.
Definition: Exceptions.hpp:98
MoFEM::Exceptions::MoFEMException
Exception to catch.
Definition: Exceptions.hpp:20
MoFEM::Exceptions::MoFEMErrorCode
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
Definition: Exceptions.hpp:56
MoFEM::Exceptions::ErrorChecker
Error check for inline function check.
Definition: Exceptions.hpp:92
MoFEM::Exceptions::MoFEMException::MoFEMException
MoFEMException(const MoFEMErrorCodes error_code, const char error_message[])
Definition: Exceptions.hpp:25
MoFEM::Exceptions::MoABErrorCode
moab::ErrorCode MoABErrorCode
MoAB error code.
Definition: Exceptions.hpp:55
MoFEM::Exceptions::MoFEMErrorCodeGeneric< PetscErrorCode >::MoFEMErrorCodeGeneric
MoFEMErrorCodeGeneric(const PetscErrorCode ierr)
Definition: Exceptions.hpp:64
MoFEM::Exceptions::MoFEMErrorCodeGeneric::MoFEMErrorCodeGeneric
MoFEMErrorCodeGeneric(const TYPE)
Definition: Exceptions.hpp:59
MoFEM::Exceptions::rval
static MoFEMErrorCodeGeneric< moab::ErrorCode > rval
Definition: Exceptions.hpp:74
MoFEM::Exceptions::MoFEMErrorCodeGeneric< PetscErrorCode >::iERR
PetscErrorCode iERR
Definition: Exceptions.hpp:63
MoFEM::Exceptions::MoFEMErrorCodeGeneric< moab::ErrorCode >::MoFEMErrorCodeGeneric
MoFEMErrorCodeGeneric(const moab::ErrorCode rval)
Definition: Exceptions.hpp:70
MoFEM::Exceptions::MoFEMExceptionRepeat::lINE
const int lINE
Definition: Exceptions.hpp:39
MoFEM
implementation of Data Operators for Forces and Sources
Definition: Common.hpp:10
MoFEM::Exceptions::MoFEMException::MoFEMException
MoFEMException(const int error_code)
Definition: Exceptions.hpp:33
MoFEM::Exceptions::MoFEMErrorCodeGeneric
Definition: Exceptions.hpp:58
MoFEM::Exceptions::MoFEMException::MoFEMException
MoFEMException(const MoFEMErrorCodes error_code)
Definition: Exceptions.hpp:23
MoFEM::Exceptions::MoFEMErrorCodeGeneric< moab::ErrorCode >::rVAL
moab::ErrorCode rVAL
Definition: Exceptions.hpp:69
MoFEM::Exceptions::ErrorChecker::operator<<
void operator<<(const moab::ErrorCode err)
Operator for handling moab::ErrorCode.
Definition: Exceptions.hpp:109
MoFEMErrorCodes
MoFEMErrorCodes
Error handling.
Definition: definitions.h:29
MoFEM::Exceptions::MoFEMException::errorMessage
char errorMessage[1024]
Definition: Exceptions.hpp:22
MoFEM::Exceptions::MoFEMException::errorCode
const int errorCode
Definition: Exceptions.hpp:21
MoFEM::Exceptions::MoFEMException::what
const char * what() const
Definition: Exceptions.hpp:30
MoFEM::Exceptions::ierr
static MoFEMErrorCodeGeneric< PetscErrorCode > ierr
Definition: Exceptions.hpp:76
MoFEM::Exceptions::MoFEMExceptionInitial::MoFEMExceptionInitial
MoFEMExceptionInitial(const int error_code, const char error_message[], const int line)
Definition: Exceptions.hpp:47
MoFEM::Exceptions::MoFEMExceptionRepeat::MoFEMExceptionRepeat
MoFEMExceptionRepeat(const int error_code, const int line)
Definition: Exceptions.hpp:40
MoFEM::Exceptions::MoFEMErrorCodeGeneric< moab::ErrorCode >
Definition: Exceptions.hpp:68
convert.int
int
Definition: convert.py:64
MOFEM_MOAB_ERROR
@ MOFEM_MOAB_ERROR
Definition: definitions.h:41
MoFEM::Exceptions::MoFEMExceptionInitial
Definition: Exceptions.hpp:46
MoFEM::Exceptions::MoFEMErrorCodeGeneric< PetscErrorCode >
Definition: Exceptions.hpp:62