views:

910

answers:

1

How a user exception class is created from standard exception?

Addressing below cases Say i have a class with some enum that indicates type of object

so based on type, member functions are available.Calling member function that is not available should throw an exception.Similarly when a getter of uninitialized is called again a exception should be thrown(I am using default argument to check for the uninitialized object).

+3  A: 

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

Also it is usually better to define a different exception for each problem rather than using an enum. This allows the try catch mechanism to catch the appropriate problems but this all depends on your exact situation and without further info it is hard to know.

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.

This smells funny (As in a Martin Fowler Smell (Check out his book)).

Martin York