• Advertisement

Archived

This topic is now archived and is closed to further replies.

Question about exception specifications

This topic is 5779 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Does an exception specification for a particular function (let's call it f() ) have to list all exceptions that can be thrown by any functions which f() calls? Here's a short example:
    
class error { };
class xerr { };

void g() throw(xerr)
{
   throw xerr();
}

// xerr listed in exception specification because

// g(), a function called from f(), can throw that type

void f() throw(error, xerr)
{
   g();
}

int main() {
   try {
      f();
   } catch (xerr) { }
}
    
By default, if I omitted xerr from f()'s specification, would that trigger a call to unexpected handler function? [edited by - fallenang3l on June 26, 2002 10:08:59 PM]

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by fallenang3l
Does an exception specification for a particular function (let''s call it f() ) have to list all exceptions that can be thrown by any functions which f() calls?

It doesn''t *have to* list them, but if f() throws an exception that is absent from a caller''s specification, then unexpected() will be called. This is a good reason in itself to avoid exception specifications, as it results in unnecessary coupling between functions. The general guideline in using exception specifications is to not use them, or to only provide an empty specification to indicate the nothrow guarantee.

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
It doesn''t *have to* list them, but if f() throws an exception that is absent from a caller''s specification, then unexpected() will be called. This is a good reason in itself to avoid exception specifications, as it results in unnecessary coupling between functions. The general guideline in using exception specifications is to not use them, or to only provide an empty specification to indicate the nothrow guarantee.


So exception specification is a useless feature (I heard rumors it''s going to change in C++0x) ? I wonder why did the standards committie add it in the first place.

Share this post


Link to post
Share on other sites
Sometimes the committee adds things that they think might be useful and it turns out otherwise, sometimes they don''t think them through sufficiently, and sometimes they omit things that obviously should be in. It mainly boils down to time.

Share this post


Link to post
Share on other sites

  • Advertisement