Heidelberg Educational Numerics Library Version 0.24 (from 9 September 2011)

src/exceptions.hh

Go to the documentation of this file.
00001 #ifndef HDNUM_EXCEPTIONS_HH
00002 #define HDNUM_EXCEPTIONS_HH
00003 
00004 #include <string>
00005 #include <sstream>
00006 
00007 namespace hdnum {
00008 
00035   class Exception {
00036   public:
00037         void message(const std::string &message); 
00038         const std::string& what() const;          
00039   private:
00040         std::string _message;
00041   };
00042 
00043   inline void Exception::message(const std::string &message)
00044   {
00045         _message = message;
00046   }
00047 
00048   inline const std::string& Exception::what() const
00049   {
00050         return _message;
00051   }
00052 
00053   inline std::ostream& operator<<(std::ostream &stream, const Exception &e)
00054   {
00055         return stream << e.what();
00056   }
00057 
00058   // the "format" the exception-type gets printed.  __FILE__ and
00059   // __LINE__ are standard C-defines, the GNU cpp-infofile claims that
00060   // C99 defines __func__ as well. __FUNCTION__ is a GNU-extension
00061 #ifdef HDNUM_DEVEL_MODE
00062 # define THROWSPEC(E) #E << " [" << __func__ << ":" << __FILE__ << ":" << __LINE__ << "]: "
00063 #else
00064 # define THROWSPEC(E) #E << ": "
00065 #endif
00066 
00084   // this is the magic: use the usual do { ... } while (0) trick, create
00085   // the full message via a string stream and throw the created object
00086 #define HDNUM_THROW(E, m) do { E th__ex; std::ostringstream th__out;            \
00087         th__out << THROWSPEC(E) << m; th__ex.message(th__out.str()); throw th__ex; \
00088   } while (0)
00089 
00099   class IOError : public Exception {};
00100 
00109   class MathError : public Exception {};
00110 
00122   class RangeError : public Exception {};
00123 
00131   class NotImplemented : public Exception {};
00132 
00139   class SystemError : public Exception {};
00140 
00144   class OutOfMemoryError : public SystemError {};
00145 
00149   class InvalidStateException : public Exception {};
00150 
00153   class ErrorException : public Exception {};
00154   
00155   // throw ErrorException with message
00156 #define HDNUM_ERROR(m) do { hdnum::ErrorException th__ex; std::ostringstream th__out;           \
00157         th__out << THROWSPEC(hdnum::ErrorException) << m; \
00158         th__ex.message(th__out.str()); \
00159         std::cout << th__ex.what() << std::endl; \
00160         throw th__ex;                              \
00161   } while (0)
00162 
00163 } // end namespace
00164 
00165 #endif