Is an exception specification needed if the method throws and catchesits own exception?

Discussion in 'C++' started by elcapitan666@gmail.com, Nov 27, 2007.

  1. Guest

    Is an exception specification need if the method itself throws and
    catches its own exception? ie

    void foo( ) throw( runtime_error )
    {
    fstream infile;

    try
    {
    infile.open( "myfile.txt" );

    if( !infile )
    throw runtime_error( "Error opening file" );
    }

    catch( runtime_error & err )
    { cout << err.what( ) << endl; }
    }
     
    , Nov 27, 2007
    #1
    1. Advertising

  2. Re: Is an exception specification needed if the method throws andcatches its own exception?

    * :
    > Is an exception specification need if the method itself throws and
    > catches its own exception? ie
    >
    > void foo( ) throw( runtime_error )
    > {
    > fstream infile;
    >
    > try
    > {
    > infile.open( "myfile.txt" );
    >
    > if( !infile )
    > throw runtime_error( "Error opening file" );
    > }
    >
    > catch( runtime_error & err )
    > { cout << err.what( ) << endl; }
    > }


    An exception specification is never neeeded, and it specifies not what
    goes on inside the function but whether and what the function can throw
    to its caller.

    The only exception specification that will pass a decent code review, is
    the empty exception specification, which documents that you don't intend
    the function to ever throw any exception.

    Personally I value clarity over micro-efficiency so from my point of
    view an empty exception specification is fine, and perhaps "throw(
    std::exception )" to assure the reader that if any exception is thrown
    it will be one compatible with std::exception, can be acceptable
    (although what it says is implicit in modern code), but any other
    exception specification indicates low quality code (because the
    programmer hasn't understood the language). It's a mystery how the
    general exception specifications came to be added to the language.
    Presumably it was a committee "political" compromise.

    Cheers, & hth.,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Nov 27, 2007
    #2
    1. Advertising

  3. Guest

    Re: Is an exception specification needed if the method throws andcatches its own exception?

    Thank you Alf, much appreciated.
     
    , Nov 27, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    2
    Views:
    473
    Chris Smith
    Jul 1, 2004
  2. Tomas
    Replies:
    17
    Views:
    2,873
    steve
    Oct 5, 2005
  3. Peter Curran
    Replies:
    1
    Views:
    3,816
    Rudy Schockaert
    Jun 24, 2003
  4. Replies:
    1
    Views:
    621
    Andrew Thompson
    Jan 28, 2007
  5. Philip Herlihy
    Replies:
    1
    Views:
    107
    Philip Herlihy
    Dec 31, 2003
Loading...

Share This Page