Actual source code: ALE_exception.hh

  1: #ifndef included_ALE_exception_hh
  2: #define included_ALE_exception_hh

  4: #include <string>
  5: #include <sstream>

  7: typedef std::basic_ostringstream<char> ostringstream;
  8: typedef std::string string;

 10: namespace ALE {
 11:   class Exception {
 12:     string _msg;
 13:   public:
 14:     //    explicit Exception(const char         * msg) : _msg(msg){};
 15:     explicit Exception(const string&        msg) : _msg(msg){};
 16:     explicit Exception(const ostringstream& txt) : _msg(txt.str()){};
 17:     Exception(const Exception& e)      : _msg(e._msg) {};
 18:     const string& msg()     const  {return this->_msg;};
 19:     const char   *message() const  {return this->_msg.c_str();};
 20:     // Printing
 21:     template <typename Stream_>
 22:     friend Stream_& operator<<(Stream_& os, const Exception& e) {
 23:       os << "ERROR: " << e.message() << std::endl;
 24:       return os;
 25:     };
 26:   };

 28:   // A helper function that throws an ALE::Exception with a message identifying the function that returned the given error code,
 29:   // including the function and the line where the error occured.
 30:   void ERROR(PetscErrorCode ierr, const char *func, int line, const char *msg);
 31:   // A helper function that allocates and assembles an error message from a format string
 32:   const char *ERRORMSG(const char *fmt, ...);
 33:   // A helper function for converting MPI errors to exception
 34:   void MPIERROR(PetscErrorCode ierr, const char *func, int line, const char *msg);
 35: }// namespace ALE

 37: // A helper macro that passes __FUNCT__ and __LINE__ with the error msg to the ERROR routine
 38: #define CHKERROR(ierr, msg) \
 39:   ERROR(ierr, __FUNCT__,  __LINE__, msg);

 41: // A helper macro that passes __FUNCT__ and __LINE__ with the error msg to the MPIERROR routine
 42: #define CHKMPIERROR(ierr, msg) \
 43:   MPIERROR(ierr, __FUNCT__,  __LINE__, msg);

 45: #endif