diff --git a/README.md b/README.md index 6aa3bb9..ecb8503 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Example #include #include -#include "exceptions.h" +#include // Use this macro to build up your hierarchy of exceptions EXCEPTION( Exception, Existential_Observation ); diff --git a/example.cpp b/example.cpp index 5b624c5..96d570e 100644 --- a/example.cpp +++ b/example.cpp @@ -5,7 +5,7 @@ #include #include -#include "exceptions.h" +#include "exceptions/exceptions.h" // Use this macro to build up your hierarchy of exceptions EXCEPTION( Exception, Existential_Observation ); diff --git a/exceptions.h b/exceptions/exceptions.h similarity index 74% rename from exceptions.h rename to exceptions/exceptions.h index e634df7..5596974 100644 --- a/exceptions.h +++ b/exceptions/exceptions.h @@ -1,5 +1,6 @@ -#ifndef __EXCEPTIONS_H__ -#define __EXCEPTIONS_H__ +#pragma once +#ifndef EXCEPTIONS_H +#define EXCEPTIONS_H #include #include @@ -19,7 +20,7 @@ EXCEPTION(Exception,Buddhist_Observation); creates an new exception class "Buddhist_Observation", derived from the base "Exception" */ -#define EXCEPTION(Super,Current) class Current : public Super {public: Current ( const std::string & desc, const std::string & func="?", const std::string & f="?", const int l=-1 ) : Super (desc,func,f,l) {name = #Current;} } +#define EXCEPTION(Super,Current) class Current : public Super {public: Current ( const std::string & desc, const std::string & func="?", const std::string & f="?", const int l=-1, const std::string & n=#Current ) : Super (desc,func,f,l,n) {} } /** A shortcut to throw an exception without having to type ",E_INFOS" @@ -35,8 +36,6 @@ class Exception : public std::exception { protected: - //! Name of the current exception class - std::string name; //! Description of the exception std::string description; @@ -50,6 +49,12 @@ protected: //! Line where the exception has been raised int line; + //! Name of the current exception class + std::string name; + + //! Assembled message + std::string message; + public: //! Constructor of the exception /*! @@ -59,21 +64,23 @@ public: Use the E_INFOS macro to raise the exception, for example : throw( Exception( "Shit evolves", E_INFOS ); */ - Exception( const std::string & desc, const std::string & func, const std::string & f, const int l ) - : description(desc), function(func), file(f), line(l) {} + Exception( const std::string & desc, const std::string & func, const std::string & f, const int l, const std::string & n = "Exception" ) + : description(desc), function(func), file(f), line(l), name(n) + { + std::ostringstream msg; + msg << "<" << name << "> " << description << "\t\t" << function << " @ " << file << ":" << line << ""; + message = msg.str(); + } //! The destructor is not allowed to throw exceptions - virtual ~Exception() throw () {} + virtual ~Exception() noexcept {} //! The method to use for printing the complete description of the exception - std::string what() + const char* what() const noexcept { - std::ostringstream msg; - msg << description << " (<" << name << "> in " << function << " at " << file << ":" << line << ")"; - - return msg.str(); + return message.c_str(); } }; -#endif // __EXCEPTIONS_H__ +#endif // EXCEPTIONS_H