 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Wil Evers Guest
|
Posted: Sat Oct 04, 2003 6:36 pm Post subject: Re: "throw exp;" - Should terminate() be called if exp throw |
|
|
In article <mXjfb.270$Qy2.49 (AT) newsread4 (DOT) news.pas.earthlink.net>, Kevin
Goodsell wrote:
| Quote: | Wil Evers wrote:
Right. So what we have is this:
struct ex1 {
ex1() { throw 42; }
};
struct ex2 {
ex2() { }
ex2(const ex2&) { throw 4711; }
};
void case1()
{
throw ex1(); // throws an int
}
void case2()
{
throw ex2(); // might result in a call to terminate()
}
This makes me wonder why case 2 isn't handled like case 1, which is to
forget about the original exception and only report the exception
escaping from the copy constructor. Calls to terminate() should be
avoided unless there is no way out, and in this case, I think there
is.
Comments?
You seem to be suggesting that an exception which occurs while another
exception is already "active" should take precedence.
|
That depends on when we consider the exception to become "active". When we
throw, the throw-expression results in a temporary. The compiler may then
decide to copy-construct that temporary, the stack unwinding machinery
takes over, and some associated handler is invoked.
The question is: is the exception considered "active" when the copy
constructor is called? If the answer is "yes", then it makes sense to call
terminate(). But if we say that the exception only becomes active after
the copy-constructor has done its job, then we have just eliminated one
reason for calling terminate().
| Quote: | Do you really feel
this is acceptable in general? My understanding is that the committee
felt that there was no good solution to this problem - there's no
general way to determine which (if either) exception is "more important"
and should continue. Either choice loses information.
|
Sure, we lose information, but I don't understand why losing information
because of an exception escaping from from the creation of the temporary is
different from losing information because of an exception escaping from the
copy-constructor.
The way things are now, using a data member (like a std::string) that has a
potentially throwing copy constructor in an exception class means we risk
killing the program.
- Wil
--
Wil Evers, DOOSYS R&D BV, Utrecht, Holland
[Wil underscore Evers at doosys dot com]
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
David Abrahams Guest
|
Posted: Sun Oct 05, 2003 6:10 am Post subject: Re: "throw exp;" - Should terminate() be called if exp throw |
|
|
[email]bouncer (AT) dev (DOT) null[/email] (Wil Evers) writes:
| Quote: | You seem to be suggesting that an exception which occurs while another
exception is already "active" should take precedence.
That depends on when we consider the exception to become "active". When we
throw, the throw-expression results in a temporary. The compiler may then
decide to copy-construct that temporary, the stack unwinding machinery
takes over, and some associated handler is invoked.
The question is: is the exception considered "active" when the copy
constructor is called? If the answer is "yes", then it makes sense to call
terminate(). But if we say that the exception only becomes active after
the copy-constructor has done its job, then we have just eliminated one
reason for calling terminate().
|
The copy-ctor may be called an arbitrary number of times during
unwinding.
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
John Potter Guest
|
Posted: Sun Oct 05, 2003 8:50 pm Post subject: Re: "throw exp;" - Should terminate() be called if exp throw |
|
|
On Sun, 5 Oct 2003 06:10:16 +0000 (UTC), [email]dave (AT) boost-consulting (DOT) com[/email]
(David Abrahams) wrote:
| Quote: | bouncer (AT) dev (DOT) null (Wil Evers) writes:
That depends on when we consider the exception to become "active". When we
throw, the throw-expression results in a temporary. The compiler may then
decide to copy-construct that temporary, the stack unwinding machinery
takes over, and some associated handler is invoked.
The copy-ctor may be called an arbitrary number of times during
unwinding.
|
My read of these two statements indicates a mismatch. Wil is talking
about the one and only temporary object constructed from the throw
expression, not any other copies made in catches. That one which gets
rethrown by throw;.
Consider:
throw E("something");
15.1/3 states that the operand of throw is like a function call
argument or the operand of return. Here we have construction of
a temporary object E which is then used to copy construct the
special temporary. As always, two temporaries may be considered
to be the same and reduced to one. If the construction of the
first temporary throws, terminate is not called. If that
construction does not throw and the copy ctor throws, terminate
is called. If the temporaries are reduced and the special
temporary is direct initialized the copy ctor does not have a
chance to throw and terminate is not called. If the direct
initializer throws, is terminate called? Consistent?
The implementations which I have checked always make a copy. It
seems that the possibility of either ctor throwing requires that
the copy be made otherwise optimization could change the meaning
of the program. Alternatively, terminate is never called during
construction of the special temporary.
John
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Wil Evers Guest
|
Posted: Wed Oct 29, 2003 6:00 pm Post subject: Re: "throw exp;" - Should terminate() be called if exp throw |
|
|
In article <uvfr4n5qk.fsf (AT) boost-consulting (DOT) com>, David Abrahams wrote:
| Quote: | bouncer (AT) dev (DOT) null (Wil Evers) writes:
When we
throw, the throw-expression results in a temporary. The compiler may
then decide to copy-construct that temporary, the stack unwinding
machinery takes over, and some associated handler is invoked.
The question is: is the exception considered "active" when the copy
constructor is called? If the answer is "yes", then it makes sense to
call terminate(). But if we say that the exception only becomes
active after the copy-constructor has done its job, then we have just
eliminated one reason for calling terminate().
The copy-ctor may be called an arbitrary number of times during
unwinding.
|
Let's see if my understanding of what the standard says about this is
correct. If it is, then the following program will either print a 0 or a
1, but never something > 1.
#include <iostream>
struct ex {
static int n_copy_ctor_calls;
ex() { }
ex(const ex&) { n_copy_ctor_calls++; }
};
int ex::n_copy_ctor_calls = 0;
int main()
{
try {
throw ex();
} catch (const ex&) {
}
std::cout << ex::n_copy_ctor_calls << std::endl;
return 0;
}
Is that correct?
- Wil
--
Wil Evers, DOOSYS R&D BV, Utrecht, Holland
[Wil underscore Evers at doosys dot com]
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|