J
Jackson A. Marshall
I have a been testing the try/catch exception mechanism
(as implemented by DigitalMars) and found that if I
throw an object its dtor gets called 3 times, if I throw
a reference its dtor gets called twice - and so it seems
practical to instantiate an object and throw its address,
and sure enough, its dtor gets called only once.
This is part of the Test class implementation (contains only
one data member: "int n" ):
volatile int count = 0;
// Constructor
Test::Test() : n(0) {
++::count;
std::cout << " Test(" << ::count << ")\n";
}
// Constructor
Test::Test(int nn) : n(nn) {
++::count;
std::cout << " Test(" << ::count << ")\n";
}
// Destructor
Test::~Test() {
std::cout << "~Test(" << ::count << ")\n";
--::count;
}
// e.g. Test1 += Test2;
const Test& Test:perator+=(const Test& rhs) {
std::cout << "operator+=(Test&)\n";
if(this == &rhs) {
Except ex("operator+= self assignment!");
throw( &ex );
}
n += rhs.n;
return *this;
}
The Except class implementation..
volatile int except = 0;
// Constructor
Except::Except(const char* msg = 0) : data(msg) {
++::except;
std::cout << " Except(" << ::except << ")\n";
}
// Destructor
Except::~Except() {
std::cout << "~Except(" << ::except << ")\n";
--::except;
}
// access data
const char* Except::Get() { return data; }
int main() {
Test t;
try { t += t; }
catch( Except* e ) {
cout << "Throw() caught: " << e->Get() << "\n";
return -1;
}
}
Output:
======
Test(1)
operator+=(Test&)
Except(1)
~Except(1)
Throw() caught: operator+= self assignment!
~Test(1)
But this implies that Except goes out of scope before
the throw is caught. I would have liked to have seen:
Test(1)
operator+=(Test&)
Except(1)
Throw() caught: operator+= self assignment!
~Except(1)
~Test(1)
Anyone comments?
TIA
(as implemented by DigitalMars) and found that if I
throw an object its dtor gets called 3 times, if I throw
a reference its dtor gets called twice - and so it seems
practical to instantiate an object and throw its address,
and sure enough, its dtor gets called only once.
This is part of the Test class implementation (contains only
one data member: "int n" ):
volatile int count = 0;
// Constructor
Test::Test() : n(0) {
++::count;
std::cout << " Test(" << ::count << ")\n";
}
// Constructor
Test::Test(int nn) : n(nn) {
++::count;
std::cout << " Test(" << ::count << ")\n";
}
// Destructor
Test::~Test() {
std::cout << "~Test(" << ::count << ")\n";
--::count;
}
// e.g. Test1 += Test2;
const Test& Test:perator+=(const Test& rhs) {
std::cout << "operator+=(Test&)\n";
if(this == &rhs) {
Except ex("operator+= self assignment!");
throw( &ex );
}
n += rhs.n;
return *this;
}
The Except class implementation..
volatile int except = 0;
// Constructor
Except::Except(const char* msg = 0) : data(msg) {
++::except;
std::cout << " Except(" << ::except << ")\n";
}
// Destructor
Except::~Except() {
std::cout << "~Except(" << ::except << ")\n";
--::except;
}
// access data
const char* Except::Get() { return data; }
int main() {
Test t;
try { t += t; }
catch( Except* e ) {
cout << "Throw() caught: " << e->Get() << "\n";
return -1;
}
}
Output:
======
Test(1)
operator+=(Test&)
Except(1)
~Except(1)
Throw() caught: operator+= self assignment!
~Test(1)
But this implies that Except goes out of scope before
the throw is caught. I would have liked to have seen:
Test(1)
operator+=(Test&)
Except(1)
Throw() caught: operator+= self assignment!
~Except(1)
~Test(1)
Anyone comments?
TIA