Question

Question from the one interview.

Please explain what does this C++ code mean:

void Foo() throw;
Was it helpful?

Solution

void Foo() throw;

This is a syntax error. The grammar for exception specification (C++98 §15.4) is:

exception-specification:

throw ( type-id-listopt )

Note that the parenthesis are required.


On the other hand,

void Foo() throw();

means the function Foo() will not throw any exceptions.

OTHER TIPS

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."

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top