Create a user exception derived from std :: exception?

How is a user exception class created from a standard exception?

Addressing below cases Let's say I have a class with some enumeration that indicates the type of object

therefore, member functions are available based on the type. You cannot use a member function that is not available to throw an exception. Similarly, when an uninitialized getter is called, an exception is thrown (I use the default argument to validate the uninitialized object).

+2
source share
1 answer

You should probably get from std :: runtime_error or one of the other standard exceptions (see <stdexcept>), and not directly from std :: exception. It defines the what () method correctly in addition to describing what exactly is happening.

It is also usually best to define a different exception for each problem, rather than using an enumeration. This allows the try catch mechanism to catch the corresponding problems, but it all depends on your specific situation and without additional information that is difficult to understand.

class myException: public std::runtime_error { public: myException(std::string const& msg): std::runtime_error(msg) {} }; 

But:

 so based on type, member functions are available.Calling member function that is not available should throw an exception. 

It smells funny (Like the smell of Martin Fowler (see his book).

+8
source

All Articles