Re: An exception that throws while it is being thrown, no terminate?
Martin B. wrote:
How do you verify that the copy-ctor is called?
Have you added some tracing, do you set a breakpoint?
Yes, when I run within Visual Studio 2008, having a breakpoint in the
copy-constructor, it gets there.
Are you running the process under the debugger?
release/debug compilation?
I tried both debug and compilation. Neither of them appear to do an
std::terminate. Both compilations produce a similar output:
First-chance exception at 0x... in MyTest.exe: Microsoft C++ exception:
std::bad_alloc at memory location 0x...
The program '[...] MyTest.exe: Native' has exited with code 1 (0x1).
What operating system are you using exactly?
Windows XP Professional.
Are you able to reproduce it? Maybe a slightly extended main() function
could be helpful, as follows:
//////////////////////////////////////////////////
int main()
{
const MyException constException("what");
try
{
// Here the copy-constructor is called:
throw constException;
}
catch( MyException & referenceToMyException )
{
// Modify the exception. So this must be a copy!
// Note that we don't actually get here.
referenceToMyException = MyException("");
}
catch( std::bad_alloc & )
{
// Here is where we get!
return EXIT_FAILURE;
}
// This line is never reached.
return 0;
}
//////////////////////////////////////////////////
Here I hope you see more clearly that constException *must* be copied, in
order to support the /potential/ assignment to referenceToMyException. (Note
that the original exception object is "const".) Also I added "return 0" at
the end, for clarity. Although it is never reached, because the program
always returns EXIT_FAILURE, when compiled on VC. Please let me know if you
agree that std::terminate should be called instead!
Kind regards,
Niels
--
Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Scientific programmer at LKEB, Leiden University Medical Center