Divick wrote:[color=blue]
> Hi all,
> I want to subclass std::exception so as to designate the type
> of error that I want to throw, out of my classes, and for that I need
> to store the messages inside the exception classes.[/color]
Or you could use several different exception classes, one for each type
of error:
throw NegativeNumberException();
throw ZeroException();
[color=blue]
> I want to use
> std::string to do that so that I don't have to deal with all the hustle
> of dealing with char *'s but as listed in the page (see link) below, it
> is not advisable to use std::string in my exception classes.[/color]
Right. You could always do something like:
throw MyException( "Negative number" );
No std::string, no hassle.
[color=blue]
> The rational given is not very clear to me but is there no other way
> out but to use char * for storing the messages ? Can somebody help me
> provide some guideline to subclass the std::exception in a clean and
> safe way?
>
>
http://www.boost.org/more/error_handling.html[/color]
The link you cite does an excellent job. Dave Abrahams is one of the
preeminent gurus when it comes to exception safety. You should listen
to him.
The rationale for not using std::string is that its copy constructor
could throw an exception. So, if you are the process of throwing your
own exception, and your exception class (or a member thereof) throws an
exception when its copy constructor is invoked, terminate() is
immediately called, which is, generally speaking, bad news. Likewise,
Abrahams notes that a constructor that throws can be bad news, but it
won't necessarily invoke terminate.
Cheers! --M