Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old January 11th, 2006, 01:35 PM
Divick
Guest
 
Posts: n/a
Default Help with Extending std::exception class

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. 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.

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

Thanks,
Divick

  #2  
Old January 11th, 2006, 02:35 PM
mlimber
Guest
 
Posts: n/a
Default Re: Help with Extending std::exception class

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

  #3  
Old January 11th, 2006, 04:35 PM
Divick
Guest
 
Posts: n/a
Default Re: Help with Extending std::exception class

>>Or you could use several different exception classes, one for each type[color=blue][color=green]
>>of error:[/color][/color]
[color=blue][color=green]
>> throw NegativeNumberException();
>> throw ZeroException();[/color][/color]

That is what I am doing right now.
[color=blue][color=green]
>>Right. You could always do something like:
>>
>> throw MyException( "Negative number" );[/color][/color]
In this case the constructor for MyException is called with the const
char * arguments, and in the constructor of MyException don't I need to
store the char * in a member char *?

Also I want to send some formatted message as well as some extra
information to the user when an exception occurs, for which I have to
pass two string values in the exception like this
MyException("errro code","error detail");

In this case what shall I do?

  #4  
Old January 11th, 2006, 06:05 PM
mlimber
Guest
 
Posts: n/a
Default Re: Help with Extending std::exception class

Divick wrote:
[snip][color=blue][color=green][color=darkred]
> >>Right. You could always do something like:
> >>
> >> throw MyException( "Negative number" );[/color][/color]
> In this case the constructor for MyException is called with the const
> char * arguments, and in the constructor of MyException don't I need to
> store the char * in a member char *?
>
> Also I want to send some formatted message as well as some extra
> information to the user when an exception occurs, for which I have to
> pass two string values in the exception like this
> MyException("errro code","error detail");
>
> In this case what shall I do?[/color]

Well, the usual way is, to derive from std::exception and override the
virtual function std::exception::what(). Alternately, you can derive
from one of its standard child classes, e.g., std::logic_error or
std::range_error, to which you pass (through the base class
constructor) a single string that will be returned by its what()
function.

If you are extending those classes, you can add whatever members you
want (though you should bear in mind their exception safety, as in the
Abrahams link above), and you can override what() (or provide a
different member function) to return a message composed from the base
class' what() and from your additional members. However, it may be just
as easy and perhaps safer (depending on how you do it) to compose the
message before before throwing the exception, and then just pass the
message to the exception class constructor.

Cheers! --M

  #5  
Old January 12th, 2006, 06:55 AM
Divick
Guest
 
Posts: n/a
Default Re: Help with Extending std::exception class

Ok that sounds good. Thanks for your help.

Divick

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles