C++ newbie - trying to understand Exceptions

T

Thomas Lenz

Could someone please consider my first prog. with a C++ Exception? Here it
is:

#include <iostream>

using namespace std;

class MyException
{ public:
MyException();
~MyException();
private:

};

MyException::MyException()
{ cout << "MyException Construktor" << endl;
}

MyException::~MyException()
{ cout << "MyException Destruktor" << endl;
}

int main()
{
try
{ throw MyException();
cout << endl;
}
catch (MyException)
{ cout << "Exception happened." << endl;
}
cout << "Ende" << endl;
}


It compiles well, but produces the following output:

MyException Construktor
Exception happened.
MyException Destruktor
MyException Destruktor
Ende


Could someone please explain why the hell the Destructor is invoked twice?

Thanks in advance, Thomas
 
D

David Harmon

Could someone please explain why the hell the Destructor is invoked twice?

When you catch the exception by value,
catch (MyException)
it is done by making a copy of it, using the copy constructor.
You have no message in the copy constructor, so you don't see it.
Then the original and the copy are both destroyed.
 
T

Thomas Lenz

David said:
On Sun, 26 Dec 2004 18:12:46 +0100 in comp.lang.c++, Thomas Lenz


When you catch the exception by value,
catch (MyException)
it is done by making a copy of it, using the copy constructor.
You have no message in the copy constructor, so you don't see it.
Then the original and the copy are both destroyed.

ok thanks, I think I got it. Is it okay to replace

catch (MyException)

with

catch (MyException&)

?

It compiles and seems to work fine on my machine, but is it "good C++" or do
I just have a gentle compiler? Or do you recommend to just don't care about
the additional MyException object?
 
D

Dave O'Hearn

Thomas said:
catch (MyException&)
[...]
It compiles and seems to work fine on my machine, but is it "good
C++" or do I just have a gentle compiler? Or do you recommend to
just don't care about the additional MyException object?

It's good C++; it is the normal way exceptions are used. Throw by
value, catch by reference:

http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.7

Catching by value has other disadvantages besides unnecessary copying.
If you create a hierarchy of exception classes, you may want to catch
by a reference to some base class, and invoke some virtual function
like msg() or describe() or whatever you call it. To properly invoke a
virtual function, you need a pointer or a reference. So catching by
reference is important for this too, not only for cutting down on
unnecessary copy operations.
 
M

MoleWang

To catch exceptions by reference may be a good style, especially when you
don't know the
exact class of the exception. On this condition, you may choose to use the
base exception
class to catch it, like that:
catch(Base& e) { }
When the exception is a derived class, you get the origin object by
reference instead of the
truncated edition by value.
 
J

jeffc

Thomas Lenz said:
MyException::MyException()
{ cout << "MyException Construktor" << endl;

I'm going to go out on a limb here and guess that you're German.
Could someone please explain why the hell the Destructor is invoked twice?

But your English appears to be just fine :)
 
J

jeffc

Thomas Lenz said:
ok thanks, I think I got it. Is it okay to replace

catch (MyException)

with

catch (MyException&)

?

It compiles and seems to work fine on my machine, but is it "good C++" or do
I just have a gentle compiler?

It is better to do it this way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top