Sign in to follow this  
beebs1

Exception Class

Recommended Posts

Heya, I've just written an simple exception class which basically functions like std::exception, but that uses unicode. I'm not sure if the class is 'safe' though, that is I don't know if there's any way it can throw an exception itself. I was hoping someone could take a quick look and let me know what they think:
class Exception
{
public:
    explicit Exception( const std::wstring msg ) { m_msgStream << msg; }
    virtual ~Exception() throw() {}

    template < typename T >
    inline Exception& operator<< ( const T &t )
    { m_msgStream << t; return *this; }

    const wchar_t *GetMsg() throw() { return m_msgStream.str().c_str(); }

private:
    std::wostringstream m_msgStream;
};

// usage
void main()
{
    Exception e( L"Foo" ); throw e;    // or...
    Exception e; e << L"Foo: " << 2; throw e;
}
I'd be grateful if someone could comment, thanks. James.

Share this post


Link to post
Share on other sites
It is possible that the allocator std::wstring and std::wostringstream use will throw std::bad_alloc()

Maybe something like this would be maybe safer...


class Exception
{
public:
Exception( );
virtual ~Exception( );

virtual void PrintMessage( wostream & out )=0;
};

class SpecificException : public Exception
{
public:
void PrintMessage( wostream & out ) { out << L"Really Bad Thing!" << std::endl; }
};


try
{
throw Specificexception;
}
catch( Exception & e )
{
e.PrintMessage( std::cout );
}


That may be safer.

Share this post


Link to post
Share on other sites
Quote:
Original post by WireAlbatross
It is possible that the allocator std::wstring and std::wostringstream use will throw std::bad_alloc()


It's a big problem (AFAIK) if an exception is thrown by the attempt to throw an exception, but it's much less of a problem if it's thrown by the attempt to *create* the exception that will be thrown (since it will just unwind that creation process and throw the bad_alloc instead of what you were going to throw). So as long as you catch by reference, I guess it's OK. (If std::bad_alloc is thrown by a std::string creation, you are probably either genuinely out of memory, or have corrupted the heap; either way, the problem you want to throw about might not be so informative as that realization. :) )

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
If std::bad_alloc is thrown by a std::string creation, you are probably either genuinely out of memory, or have corrupted the heap


In either case, there's not a whole lot I could do except bail out of the program.

Edit:
Quote:
Original post by Dave
Why not just derrive your own exception from std::exception and give it a std::wstring member?


I thought about it, but it didn't seem right as I wasn't going to use the (only) member function that std::exception provides. Please comment if you think this is not a valid argument!

Thanks.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this