Niels Dekker - no reply address
2009-12-02 19:52:33 UTC
When the copy-constructor of an object throws an exception while the object
itself is being thrown, I'd expect std::terminate to be called. But it
doesn't seem to happen! At least, not when I use Visual C++. I've tried
both VC 2008 and VC 2010 Beta 1. Is that a known compiler bug? I can't find
it at https://connect.microsoft.com/VisualStudio
Typical example: suppose I have an exception class, MyException, containing
an std::string. This is bad practice, of course, because std::string may
itself throw an exception, while it is being copied. Instead of waiting for
std::string to throw, I simply added "throw std::bad_alloc()" to the
copy-constructor of MyException, for the sake of the test. Please have a
look:
//////////////////////////////////////////////////
#include <cstdlib>
#include <stdexcept>
#include <string>
class MyException: public std::logic_error
{
std::string m_string;
public:
MyException(const std::string& arg)
:
std::logic_error(arg), m_string(arg) {}
// A throwing copy-constructor!
MyException(const MyException& arg)
:
std::logic_error(arg), m_string(arg.m_string)
{
// Triggering std::terminate...?
throw std::bad_alloc();
}
~MyException() throw() { }
};
int main()
{
const MyException constException("what");
try
{
// Here the copy-constructor is called:
throw constException;
}
catch( std::bad_alloc & )
{
// Here is where we get!
return EXIT_FAILURE;
}
}
//////////////////////////////////////////////////
Shouldn't the above test program call std::terminate, instead of catching
the std::bad_alloc?
Kind regards,
Niels
--
Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Scientific programmer at LKEB, Leiden University Medical Center
itself is being thrown, I'd expect std::terminate to be called. But it
doesn't seem to happen! At least, not when I use Visual C++. I've tried
both VC 2008 and VC 2010 Beta 1. Is that a known compiler bug? I can't find
it at https://connect.microsoft.com/VisualStudio
Typical example: suppose I have an exception class, MyException, containing
an std::string. This is bad practice, of course, because std::string may
itself throw an exception, while it is being copied. Instead of waiting for
std::string to throw, I simply added "throw std::bad_alloc()" to the
copy-constructor of MyException, for the sake of the test. Please have a
look:
//////////////////////////////////////////////////
#include <cstdlib>
#include <stdexcept>
#include <string>
class MyException: public std::logic_error
{
std::string m_string;
public:
MyException(const std::string& arg)
:
std::logic_error(arg), m_string(arg) {}
// A throwing copy-constructor!
MyException(const MyException& arg)
:
std::logic_error(arg), m_string(arg.m_string)
{
// Triggering std::terminate...?
throw std::bad_alloc();
}
~MyException() throw() { }
};
int main()
{
const MyException constException("what");
try
{
// Here the copy-constructor is called:
throw constException;
}
catch( std::bad_alloc & )
{
// Here is where we get!
return EXIT_FAILURE;
}
}
//////////////////////////////////////////////////
Shouldn't the above test program call std::terminate, instead of catching
the std::bad_alloc?
Kind regards,
Niels
--
Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Scientific programmer at LKEB, Leiden University Medical Center