tags:

views:

272

answers:

3

So I am a little confused, I have been looking around trying to determine an appropriate way of inheriting from std::exception for my own type. Now according to cplusplus.com (and i know this isn't necessarily the standard, thats why I'm asking), std::exception is a base class with no members. However, after looking at my implementation of the standard library (VS 2005), the class std::exception is quite clearly defined with two member variables:

class _CRTIMP_PURE exception
    { // base of all library exceptions
        ... 
private:
    const char *_m_what;
    int _m_doFree;
    };

Now is this standard to have these members included within the base class std::exception, my understanding was that it isn't. My reason for asking is that my derived exception class really only needs a single string for its information, thus this implementation would work fine. However, I'm worried that this is non-standard, and as such I might be better off inheriting from std::exception and adding the appropriate members + copy/assignment code myself.

+6  A: 

The std::exception class is defined as:

namespace std { 
    class exception { 
    public: 
        exception() throw(); 
        exception(const exception&) throw(); 
        exception& operator=(const exception&) throw(); 
        virtual ~exception() throw(); 
        virtual const char* what() const throw(); 
    }; 
}

That is the contract that you have to live up to when it comes to substitutability. The Standard defines the specifics of each member. The only one that usually requires consideration is that what() returns a "null-terminated multi-byte string, suitable for display as a wstring". The other part is obeying and ensuring the throw() contract.

D.Shawley
That actually was another question of mine, what exactly do you mean by the 'throw() contract'. I assume it has something to do with appropriately throwing an additional exception if the exception class itself in some way does something wrong?
DeusAduro
throw() means that your implementation must not throw any exception.
Brian Neal
Emphasis on the ''MUST NOT''
Martin York
+2  A: 

To make your own exception class, you want to overload the following function:

virtual const char* what() const throw()

If you look at the Visual Studio implementation, exception uses malloc to allocate memory to store the character string. If that memory isn't available, _m_what is set to a predefined string. _m_doFree is a flag that stores whether free needs to be called on this memory.

rlbond
Thanks, and the implementation you had up there is the same as mine. My question is more along the lines of, "I see that VS has implemented the 'what()' capability in the base class, if I am to make use of this, will I run into compatibility issues with another imp of the std library?"
DeusAduro
@DeusAduro: No. Because they are private members you have no access to them. If you stick to using the public interface the internal private members will not change how it works across different implementations.
Martin York
+1  A: 

I would look at the other standard exceptions aswell.
std::exception may be the base class but other standard exceptions provide some other features.

I usually derive from std::runtime_error

Martin York