C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

What is the meaning of the no throw?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Julius
Guest





PostPosted: Wed Jan 12, 2005 9:13 pm    Post subject: What is the meaning of the no throw? Reply with quote



Hello people,

I am just curious of meaning of throw()? Like in function signature:
void foo(void) throw();

I understand that it means that this certain function will never throw
any exception. Right?

But why can I in the body of the function explicetely throw? At least
VS7 allow this I have only warning, no more.

For example:

void foo (void ) throw()
{
throw true;
}

Thanks in advance.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Victor Bazarov
Guest





PostPosted: Thu Jan 13, 2005 12:08 pm    Post subject: Re: What is the meaning of the no throw? Reply with quote



"Julius" <yuliy (AT) gmx (DOT) de> wrote...
Quote:
I am just curious of meaning of throw()? Like in function signature:
void foo(void) throw();

It's a promise that the function does not throw any C++ exceptions.

Quote:
I understand that it means that this certain function will never throw
any exception. Right?

Right.

Quote:
But why can I in the body of the function explicetely throw? At least
VS7 allow this I have only warning, no more.

For example:

void foo (void ) throw()
{
throw true;
}

It is expressly allowed to throw exceptions that the function promises not
to throw (in this case, any). The behaviour is to call 'unexpected()' if
the exception is thrown that is not declared in the function's exception
specification (see 15.4)

Victor



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
ntrifunovic@hotmail.com
Guest





PostPosted: Thu Jan 13, 2005 12:08 pm    Post subject: Re: What is the meaning of the no throw? Reply with quote



Look here for an excellent explanation:
http://www.gotw.ca/publications/mill22.htm


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Markus Moll
Guest





PostPosted: Thu Jan 13, 2005 12:12 pm    Post subject: Re: What is the meaning of the no throw? Reply with quote

Hi

Julius wrote:

Quote:
I understand that it means that this certain function will never throw
any exception. Right?

Yes. But see below...

Quote:
But why can I in the body of the function explicetely throw? At least
VS7 allow this I have only warning, no more.

There's no problem with that.
Consider the more general case of non-empty throw-specifiers.
Whenever an exception would leave a block with a throws specifier
disallowing the type of that exception, the "std::unexpected()" function is
called. You can set your own handler via "set_unexpected(unexpected_handler
h)". The standard unexpected handler tries to throw a std::bad_exception
object, if this is also disallowed it calls "terminate()".

Try the following:

#include <iostream>
void f() throw (int) { throw 1.0; }
void whoa() { throw 42; }
int main()
{
set_unexpected(whoa);
try { f(); } catch(int i) { std::cout << i << std::endl; }
}

Markus

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
L.Suresh
Guest





PostPosted: Thu Jan 13, 2005 4:38 pm    Post subject: Re: What is the meaning of the no throw? Reply with quote

Right, it is a strong guarantee that it wont throw anything. If it
violates this guarantee then std::unexpected() is called which by
default calls std::terminate() which in turn calls abort().

The example you have given is equivalent to ...

void foo() throw()
{
try {
throw true;
} catch (...) {
std::unexpected();
}
}

--lsu


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
L.Suresh
Guest





PostPosted: Thu Jan 13, 2005 4:39 pm    Post subject: Re: What is the meaning of the no throw? Reply with quote

Right, it is a strong guarantee that it wont throw anything. If it
violates this guarantee then std::unexpected() is called which by
default calls std::terminate() which in turn calls abort().

The example you have given is equivalent to ...

void foo() throw()
{
try {
throw true;
} catch (...) {
std::unexpected();
}
}

--lsu


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Thomas Richter
Guest





PostPosted: Thu Jan 13, 2005 5:10 pm    Post subject: Re: What is the meaning of the no throw? Reply with quote

Hi,

Quote:
I am just curious of meaning of throw()? Like in function signature:
void foo(void) throw();

"Dear compiler, I promise not to throw in this routine and explicitly
allow you to take advantage of this".

Thus, usually not much better than a comment, IMHO, except that it
possibly might (or might not) help the compiler a bit.

Quote:
I understand that it means that this certain function will never throw
any exception. Right?

It means that you hand out a contract to the compiler that this
function never throws. If it does, anything could happen.

Quote:
But why can I in the body of the function explicetely throw?

Because whether a function is able to throw or not is out of the
control of the compiler anyhow. You could call other functions that
might or might not throw, possibly in external libraries. Or, what
about the following: Will this function satisfy the contract of not
throwing ?

#include <cmath>

void foo(void) throw()
{
if (sqrt(9.0) != 3.0)
throw true;
}

or, if that sounds too obvious, what about this one:

#include <cmath>

void foo(void) throw()
{
if (sin(3.14159326) != 0.0)
throw true;
}

IMHO, throw() is not a very wise construction. Avoid it.

So long,
Thomas


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
GianGuz
Guest





PostPosted: Thu Jan 13, 2005 5:18 pm    Post subject: Re: What is the meaning of the no throw? Reply with quote

Julius wrote:
Quote:
Hello people,

I am just curious of meaning of throw()? Like in function signature:
void foo(void) throw();

I understand that it means that this certain function will never
throw
any exception. Right?

But why can I in the body of the function explicetely throw? At least
VS7 allow this I have only warning, no more.

For example:

void foo (void ) throw()
{
throw true;
}

Thanks in advance.

Throw list specification is always a bad idea. When a function is
marked with
a throw specifier some upsetting things happened:

1) The throw(x,y,...,z) signature becomes part of the function type -
so f() throw() is different from f()

2) The compiler is 'suggested' to optimize code only for
exceptions hinted by the throw list and if it is able to infer that
really no exception will be throw (i.e. f() throw() ) it will remove
the exception handling into that scope.

3) 'An "unexpected exception" is called when a function throws an
exception it promised not to throw, in violation of its exception
specification. The unexpected exception may be replaced by a
bad_exception object during the stack unwind' [ Schmidt ]

As you pointed out, nobody prevents you to throw any exceptions into
your functions even if it is forbidden by the exception specification
(problem 3).
By the way the compiler is able to remove useless try catcth block
without
exceptions specification (problem 2).
So use that language feature only when you have to interface to code
that is using it and you can't avoid type mismatching (problem 1).

Gianguglielmo
[excess quoting deleted --mod]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Victor Bazarov
Guest





PostPosted: Thu Jan 13, 2005 9:59 pm    Post subject: Re: What is the meaning of the no throw? Reply with quote

L.Suresh wrote:
Quote:
Right, it is a strong guarantee that it wont throw anything. If it
violates this guarantee then std::unexpected() is called which by
default calls std::terminate() which in turn calls abort().

'unexpected' is allowed to re-throw instead of calling 'terminate'.

V

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Markus Moll
Guest





PostPosted: Sat Jan 15, 2005 3:33 am    Post subject: Re: What is the meaning of the no throw? Reply with quote

Hi

Thomas Richter wrote:

Quote:
It means that you hand out a contract to the compiler that this
function never throws. If it does, anything could happen.

No. The behaviour is defined.

Quote:
IMHO, throw() is not a very wise construction. Avoid it.

IMHO, it's the right thing to do if you
a) do not want that function to throw anything
b) want to let the reader know that it's useless to try to catch any
exceptions here
c) want to force derivations not to throw anything either

For example, this gives you the opportunity to have simple exception-safe
code, eg in
void f(SomeClass*) throw();
SomeClass *p = new SomeClass();
f(p);
delete p;

(Yes, there _is_ std::auto_ptr... and yes, the example is kind of
pointless...)

Worst thing that could happen is the program terminates.

Markus


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Dave Moore
Guest





PostPosted: Sat Jan 15, 2005 3:42 am    Post subject: Re: What is the meaning of the no throw? Reply with quote


"Thomas Richter" <thor (AT) cleopatra (DOT) math.tu-berlin.de> wrote

Quote:
Hi,

I am just curious of meaning of throw()? Like in function signature:
void foo(void) throw();

"Dear compiler, I promise not to throw in this routine and explicitly
allow you to take advantage of this".

No, the no-throw guarantee says that, "This function is *always* expected to
return normally. If it does not, it means that something outside the subset
of events this software is designed to handle has happened."

Quote:
Thus, usually not much better than a comment, IMHO, except that it
possibly might (or might not) help the compiler a bit.

It is much better than a comment, because it activates a fail-safe
mechanism, namely calling std::unexpected(), immediately at the point that
the unexpected event happened. This may be more appropriate than allowing
the unexpected exception to propagate up the stack.

Basically your position (which seems to be shared by many other
respondants in this thread), is tantamount to saying "All exceptions are
unrecoverable errors, so don't worry about classifying them (except for
returning error messages perhaps)." The no-throw guarantee, and exception
specifications in general, provide a built-in way for a program to separate
"Ok, that is exceptional behavior I can (maybe) deal with." from "Holy crap,
what the heck just happened!!". To replicate such a mechanism without it,
you would have to call std::unexpected() manually from inside a catch(...)
block after every function call that meets the criterion "should never fail
to return".

Quote:
I understand that it means that this certain function will never throw
any exception. Right?

It means that you hand out a contract to the compiler that this
function never throws. If it does, anything could happen.

Wrong. The behavior is quite specifically defined: std::unexpected() is
called, which by default in turn calls std::terminate(). However the user
can override this behavior by specifying an alternate handler using
set_unexpected().

[snip]

Quote:
IMHO, throw() is not a very wise construction. Avoid it.

Well, the developers of the standard library would disagree I guess, because
they used no-throw exception specifications quite frequently. OTOH, I have
never needed to use them myself, but then I have never had to deal with
anything more critical than handling errors in user-input, or floating point
exceptions.

Perhaps better advice would be, "make sure a throw() specification is really
warranted, and that you understand the ramifications", which boils down to
"think carefully about your design". I guess no one will argue with that
8*).

Dave Moore



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
L.Suresh
Guest





PostPosted: Sat Jan 15, 2005 4:10 am    Post subject: Re: What is the meaning of the no throw? Reply with quote

Quote:
'unexpected' is allowed to re-throw instead of calling 'terminate'.

15.5.1, terminate() is called when
"- when the implementation's default unexpected_handler is called"

The default behaviour is to call std::terminate(). Of course, you can
write your own unexpected_handler that does things differently.

--lsu


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
natkhatbandar@yahoo.com
Guest





PostPosted: Sun Jan 16, 2005 4:14 am    Post subject: Re: What is the meaning of the no throw? Reply with quote

Just FYI:
VC++ 6.0 does not call unexpected() at all !!!
The function that calls foo() will be able to catch the exception of
type bool.
The documentation admits that unexpected() is never called by the
exception-handling run-time library.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Thomas Richter
Guest





PostPosted: Tue Jan 18, 2005 11:13 pm    Post subject: Re: What is the meaning of the no throw? Reply with quote

Hi Dave,

Quote:
"Dear compiler, I promise not to throw in this routine and explicitly
allow you to take advantage of this".

No, the no-throw guarantee says that, "This function is *always* expected to
return normally. If it does not, it means that something outside the subset
of events this software is designed to handle has happened."

Thus, usually not much better than a comment, IMHO, except that it
possibly might (or might not) help the compiler a bit.

It is much better than a comment, because it activates a fail-safe
mechanism, namely calling std::unexpected(), immediately at the point that
the unexpected event happened. This may be more appropriate than allowing
the unexpected exception to propagate up the stack.

Thanks, I stand corrected.

Quote:
Basically your position (which seems to be shared by many other
respondants in this thread), is tantamount to saying "All exceptions are
unrecoverable errors, so don't worry about classifying them (except for
returning error messages perhaps)."

Not quite. My position is that somewhere in the main, you need to put
a catch() and print there that you caught an exception that shouldn't have
propagated up to that level. (-;

Quote:
The no-throw guarantee, and exception
specifications in general, provide a built-in way for a program to separate
"Ok, that is exceptional behavior I can (maybe) deal with." from "Holy crap,
what the heck just happened!!".

Strange point of view... If I can deal with the exception, I put a catch(...),
and no throw(). If the function throws despite a throw(), it breaks its
contract to the caller. If I would want to get a handler called, I could
have done that myself as an error reporting mechanism in first place.

Quote:
To replicate such a mechanism without it,
you would have to call std::unexpected() manually from inside a catch(...)
block after every function call that meets the criterion "should never fail
to return".

Yes, exactly. Except that a function that should never fail should
never call std::unexpected either. It is just a different mechanism of
reporting an exceptional condition for something the function promised
not to cause an exception in first place. That is, I believe there's a
basic design flaw in std::unexpected(): It is a way of bypassing the
contract of the function.

Quote:
It means that you hand out a contract to the compiler that this
function never throws. If it does, anything could happen.

Wrong. The behavior is quite specifically defined: std::unexpected() is
called, which by default in turn calls std::terminate(). However the user
can override this behavior by specifying an alternate handler using
set_unexpected().

Thanks for the clarification.

Quote:
IMHO, throw() is not a very wise construction. Avoid it.

Well, the developers of the standard library would disagree I guess, because
they used no-throw exception specifications quite frequently. OTOH, I have
never needed to use them myself, but then I have never had to deal with
anything more critical than handling errors in user-input, or floating point
exceptions.

Perhaps better advice would be, "make sure a throw() specification is really
warranted, and that you understand the ramifications", which boils down to
"think carefully about your design". I guess no one will argue with that
8*).

True enough. (-:

So long,
Thomas


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.