Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Is an exception specification needed if the method throws and catchesits own exception? (http://www.velocityreviews.com/forums/t555346-is-an-exception-specification-needed-if-the-method-throws-and-catchesits-own-exception.html)

elcapitan666@gmail.com 11-27-2007 01:42 AM

Is an exception specification needed if the method throws and catchesits 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; }
}

Alf P. Steinbach 11-27-2007 01:59 AM

Re: Is an exception specification needed if the method throws andcatches its own exception?
 
* elcapitan666@gmail.com:
> 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?

elcapitan666@gmail.com 11-27-2007 04:01 AM

Re: Is an exception specification needed if the method throws andcatches its own exception?
 
Thank you Alf, much appreciated.


All times are GMT. The time now is 09:14 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.