R
raj s
What is the advantage and disadvantage of using function declaration
with exceptions specified.
like foo()throws(char&)
with exceptions specified.
like foo()throws(char&)
What is the advantage and disadvantage of using function declaration
with exceptions specified.
like foo()throws(char&)
What is the advantage and disadvantage of using function declaration
with exceptions specified.
like foo()throws(char&)
What is the advantage and disadvantage of using function declaration
with exceptions specified.
like foo()throws(char&)
Exceptions are always specified.
If foo() did not have an explicit exception specification then it
could throw any kind of exception.
void foo() { } // throws(anything)
however,
foo() throws() { }
then the contract says foo shall throw no exception.
If the program breaks the contract C++ calls terminate().
In your example, if foo threw anything else but a char, terminate()
gets called.
What is the advantage and disadvantage of using function declaration
with exceptions specified.
like foo()throws(char&)
On 2008-09-15 11:54, raj s wrote:
If you have a function with an exception specification you can work
under the assumption that the function only throws exceptions of the
specified kind. In other words you do not have to check for other kinds
of exceptions in your handlers.
The drawback is that if an exception not listed in the specification is
thrown in the function (or by a function called by the function) which
is not also caught in the function, your program will call unexpected()
which will usually terminate your application.
It specifies a contract, enforced by the compiler. The contract
is a negative one, however: the exception specification
guarantees that you will not get any exceptions of the given
type from the function, not that the function will actually
throw any of the listed exceptions when appropriate.
Uh, James, I think you got that backward. I believe that
15.4/7 says that the function is allowed to throw the listed
exceptions, anything else gets routed to unexpected(), per
15.4/8.
raj said:What is the advantage and disadvantage of using function declaration
with exceptions specified.
like foo()throws(char&)
How is that a drawback? It's more or less the same behavior as
an assertion failure.
What is the advantage and disadvantage of using function declaration
with exceptions specified.
like foo()throws(char&)
It is a drawback because the exception specifications are not
checked at compile-time.
So when you a year later makes a change in one part of the
code and miss the fact that one of the functions have a
specification you run the risk of getting some unpleasant
surprises.
Don't get me wrong, I like the idea that you can limit which
exceptions can be thrown but with the current implementation
its usefulness is limited to basic functions and very hard to
apply to more general functions.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.