CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Threaded View

  1. #10
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by younger2
    Thank you, dumah, I tried your sample and still a little confused about your word"throw() after a function means no exceptions will be thrown......not showing an exception specifier means the func can throw anything....".
    The form of this function declaration is called exception specification. The essential benefit of an exception specification is that clients of a component know exactly what exceptions may be thrown from a function or a method. This can be great from the client perspective of handling exceptions, because the client knows exactly what exceptions, if any, may be thrown.

    Consider the following code:
    Code:
    class CFoo
    {
    public:
    void   FunctionWithOneExceptionSpecification() throw (ExceptionClass);
    void   FunctionThatThrowsNoExceptions() throw();
    };
    The first exception specification says that exceptions of type 'ExceptionClass' or derived from 'ExceptionClass' may be thrown from that method.

    However, to implement this specification you will most likely have to use a 'try'/'catch' block within the implementation of these methods to enforce the exception specification. Otherwise you run the risk of 'std::unexpected()' being called, the default behavior of which is to terminate the application. And since aborting the program execution is usually much more catastrophic than letting an exception propagate all the way up you have to fall back on using the 'try' block.

    The second function is declared as not throwing any kind of exception. However, if you did not write the function itself you will never know if this function still can throw an exception. Then you need to have a serious discussion with the developer of the function about exception handling...
    Last edited by Andreas Masur; November 7th, 2002 at 09:45 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured