views:

226

answers:

4

Question from the one interview.

Please explain what does this C++ code mean:

void Foo() throw;
+2  A: 

void Foo() throw; is ill-formed.

Instead void Foo() throw(); means that function 'Foo' is declared such that it does not throw any exception

$15.4/10 - "A function with no exception-specification allows all exceptions. A function with an empty exceptionspecification, throw(), does not allow any exceptions."

Chubsdad
Note that the exception spec is checked at **runtime**, which implies some overhead, and causes std::bad_exception (which usually results in the `unexpected` function being called) to be thrown if one tries to throw a non-matching exception.
Billy ONeal
+10  A: 
KennyTM
It's not polite to throw your Foo(). Sorry. I just could not help myself.
gregg
The parentheses on the throw are significant. You name the exception types that may be thrown inside them. If they are empty, as shown, it does mean that no ("expected") exceptions are be thrown by Foo(). There are differing opinions on the use of exception specs. Here's one: http://www.gotw.ca/publications/mill22.htm.
gregg
A: 

in an interview, this question provides a good opportunity to talk about exception checking; the differences between C++ and Java; your personal preferences and opinions; a time when exception checking has hurt or helped, etc. It's the kind of question that is not so much about the literally correct single sentence answer that states the syntax rule, but more about an invitation to talk about the way you program and think. Could be dangerous if you pretend to know more than you do, or if your philosophy is very different from theirs.

Kate Gregory
This should be a comment, as it doesn't answer the question.
cake
It answers the question "what should I say when I am asked this in an interview?" which is different from "I came across this in another guy's code - what does it mean?"
Kate Gregory
A: 

It means the developer does not use meaningful function names...

JohnMcG