// CTORException.cpp : Defines the entry point for the console application.
//
#include <memory>
#include <stdexcept>
#include <iostream>
using namespace std;
class object_t
{
public:
object_t()
{
cout << "object_t CTOR" << endl;
}
~object_t(){
cout << "object_t DTOR" << endl;
}
};
class test_t
{
public:
test_t() try
: ptr(nullptr) //NOTE that I start with member which cannot throw
, mem(new object_t)
{
cout << "test_t CTOR" << endl;
ptr = new object_t;
throw logic_error("exception just to test...");
}
catch (...)
{
cout << "here we handle our dynamic allocation" << endl;
if (ptr != nullptr)
delete ptr;
throw;
}
~test_t()
{
cout << "test_t DTOR" << endl;
delete ptr;
}
private:
object_t* ptr;
unique_ptr<object_t> mem;
};
int main()
{
try
{
test_t t;
}
catch (exception& roEx)
{
cout << roEx.what() << endl;
}
return 0;
}