Dont Declare Thrown Exceptions

In C++, throw() clauses on declarations are just asking for trouble. They are not checked at compile time, so you don't get the safety that you get from Java Checked Exceptions. They are checked at run-time, however, which imposes a runtime overhead on the code for the checking, and provides an opportunity for unexpected calls to abort(). e.g.

 void f() throw(A,B)
 {
     g();
     h();
 }

is directly equivalent to

 void f()
 {
     try
     {
         g();
         h();
     }
     catch(A&)
     {
         throw;
     }
     catch(B&)
     {
         throw;
     }
     catch(...)
     {
         std::unexpected(); // eventually calls abort()
     }
 }

throw() clauses also create problems when trying to use virtual functions (as they impose themselves on overrides in derived classes), or pointers to functions (as they must be declared as part of the pointer type).

Basically, avoid them at all costs.


CategoryCpp | CategoryException


EditText of this page (last edited June 11, 2005) or FindPage with title or text search