The correct way to inherit from std :: exception

I just created an exception hierarchy and wanted to pass char* constructor of one of my derived classes with a message saying what's wrong, but apparently std::exception does not have a constructor that would allow me to do this. However, there is a member of the what() class, which suggests that some information may be passed on.
How can I (can I?) Pass text to a std::exception derived class to pass information to my exception class, so I can say somewhere in the code:

 throw My_Exception("Something bad happened."); 
+48
c ++ exception
Nov 16 2018-11-11T00:
source share
5 answers

If you want to use the string constructor, you must inherit from std :: runtime_error or std :: logic_error , which implements the string constructor and implements std :: exception :: which method.

Then this is just the case of calling the runtime_error / logic_error constructor from your new inherited class, or if you are using C ++ 11, you can use constructor inheritance.

+51
Nov 16 2018-11-11T00:
source share

I use the following class for my exceptions, and it works great:

 class Exception: public std::exception { public: /** Constructor (C strings). * @param message C-style string error message. * The string contents are copied upon construction. * Hence, responsibility for deleting the char* lies * with the caller. */ explicit Exception(const char* message): msg_(message) { } /** Constructor (C++ STL strings). * @param message The error message. */ explicit Exception(const std::string& message): msg_(message) {} /** Destructor. * Virtual to allow for subclassing. */ virtual ~Exception() throw (){} /** Returns a pointer to the (constant) error description. * @return A pointer to a const char*. The underlying memory * is in posession of the Exception object. Callers must * not attempt to free the memory. */ virtual const char* what() const throw (){ return msg_.c_str(); } protected: /** Error message. */ std::string msg_; }; 
+56
Nov 16 2018-11-11T00:
source share

How about this:

 class My_Exception : public std::exception { public: virtual char const * what() const { return "Something bad happend."; } }; 

Or create a constructor that accepts a description if you want ...

+7
Nov 16 2018-11-11T00:
source share

The what method is virtual, and the point is that you must override it to return any message you want to return.

+5
Nov 16 2018-11-11T00:
source share

If your goal is to throw an exception so that you do not throw a universal exception ( cpp: S112 ), you might just want to provide the exception you inherited from ( C ++ 11 ) using the using declaration.

Here is a minimal example for this:

 #include <exception> #include <iostream> struct myException : std::exception { using std::exception::exception; }; int main(int, char*[]) { try { throw myException{ "Something Happened" }; } catch (myException &e) { std::cout << e.what() << std::endl; } return{ 0 }; } 

As Kilian points out in the comment section, the example depends on the particular implementation of std :: exception, which offers more constructors than mentioned here .

To avoid this, you can use any of the convenient classes predefined in the <stdexcept> header. Check out these Exception Categories for inspiration.

+5
Jul 12 '18 at 20:18
source share



All Articles