Usenet comp.unix.osf.misc (1992-2004)
 help / color / mirror / Atom feed
From: "Gene Thompson" <gthompson@pin-corp.com>
Subject: Re: Strange thing about exceptions and destructors
Date: 1997/05/12	[thread overview]
Message-ID: <01bc5f1f$1e545740$8679d5ce@pin1113.pin-corp.com> (raw)
In-Reply-To: gl7mh4mt4y.fsf@bjesomar.srce.hr

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 3140 bytes --]


A,

There are quite a few things going on here.
1. You are catching you exception class by value. This makes 2 copys of the
object thrown.
2. You do not see this as you did not override the copy ctor as well.
3. g++ doesn't implement exception handling well, so don't use it for them.
4. Because of #1, the most correct is the DEC solution.

When you pass an exception object by value, the object is copied to a
temporary because the object you explicitly created goes out of scope when
the throw is executed. Then when you catch the temporary, it must be copied
into your local variable (in this case, 'a'). Thus you get 3 constructors
(2 copy and one explicit) and 3 destructors. You should change your catch
to use 'a' by reference. This way it is only copied once (you cannot get
around the throw copy).

Just as an aside, use "\n" in your cout calls does NOT flush the output
stream so you may not always get what you expect. You should use the endl
manipulator :
cout << "Hello World" << endl; 	// Executes a new line and flushes the
stream.

Good Luck, 
Gene Thompson

PS. For a good discussion on exceptions, see "More Effective C++" by Scott
Meyers.


Aleksandar Milivojevic <alex@srce.hr> wrote in article
<gl7mh4mt4y.fsf@bjesomar.srce.hr>...
> I would like to know when the destructor for thrown object is called.  I
> expected that it is called only one time (and not two or three times)
> after the catch block is executed.  So in order to check that, I wrote
> a simple program and it gave me three different outputs when compiled
> with Sun CC, DEC cxx and GNU g++ (last gave me internal compiler
> error, but when I commented last line of main() it compiled OK).
> 
> Now, Am I doing something totaly wrong, or that three compilers
> generate incorect code (and which one generated correct code, if any)?
> 
> Here is program, and the outputs it gives when compiled with before
> mentioned compilers:
> 
> #include <iostream.h>
> 
> class exp {
> public:
>   exp(int x);
>   ~exp();
>   int i;
> };
> 
> exp::exp(int x) {
>    cout << "Constructor exp(" << x << ")\n";
>    i = x;
> }
> 
> exp::~exp() {
>    cout << "Destructor ~exp()\n";
> }
> 
> main() {
>   try {
>     cout << "Throwing exception\n";
>     throw exp(1);
>   } catch (exp a) {
>     cout << "Catched exp(" << a.i << ")\n";
>   }
>   cout << "Exiting\n";
> }
> 
> When compiled with Sun CC:
> Throwing exception
> Constructor exp(1)
> Catched exp(1)
> Destructor ~exp()
> Destructor ~exp()
> Exiting
> 
> When compiled with DEC cxx:
> Throwing exception
> Constructor exp(1)
> Destructor ~exp()
> Catched exp(1)
> Destructor ~exp()
> Destructor ~exp()
> Exiting
> 
> When compiled with GNU g++ (last line of main() commented):
> Throwing exception
> Constructor exp(1)
> Destructor ~exp()
> Catched exp(1)
> 
> -- 
> Aleksandar Milivojevi�                        http://jagor.srce.hr/~alex/
>                            e-mail: alex@srce.hr
> ================================ooooO=Ooooo==============================
> 
>   Real Users never know what they want, but they always know when your
>                        program doesn't deliver it.
> 



  parent reply	other threads:[~1997-05-12  4:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-05-12  4:00 Strange thing about exceptions and destructors Aleksandar Milivojevic
1997-05-12  4:00 ` Partha Sinha
1997-05-12  4:00 ` Doug Harrison
1997-05-12  4:00 ` Allen Hadden
1997-05-12  4:00 ` Gene Thompson [this message]
1997-05-13  4:00 ` Philip Staite
1997-05-14  4:00 ` Aleksandar Milivojevic
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox