Construction and destruction of objects when exceptions are thrown

Discussion in 'C++' started by thamizh.veriyan@gmail.com, Feb 20, 2007.

  1. Guest

    I compiled this code on gcc 4.1.2 and the output I got seems to be a
    bit weird.

    Here is the code:

    #include <iostream>

    class A{
    public:
    A(){std::cout << "Constructor" << std::endl;}
    ~A(){std::cout << "Destructor" << std::endl;}
    };

    void display(){
    A mystr;
    throw mystr;
    }

    int main() {
    try{
    display();
    }
    catch(A str){
    }
    return 0;
    }

    The output happens to be:
    Constructor
    Destructor
    Destructor
    Destructor

    I don't know if I am missing something. I checked all the references
    that I have for exceptions. Looks like local objects get deleted
    normally when exceptions are thrown. I cant seem to explain this
    output. Would be grateful if the members here can help me with this.

    Regards
    Dana
     
    , Feb 20, 2007
    #1
    1. Advertising

  2. wrote:
    > I compiled this code on gcc 4.1.2 and the output I got seems to be a
    > bit weird.
    >
    > Here is the code:
    >
    > #include <iostream>
    >
    > class A{
    > public:
    > A(){std::cout << "Constructor" << std::endl;}
    > ~A(){std::cout << "Destructor" << std::endl;}
    > };
    >
    > void display(){
    > A mystr;
    > throw mystr;
    > }
    >
    > int main() {
    > try{
    > display();
    > }
    > catch(A str){
    > }
    > return 0;
    > }
    >
    > The output happens to be:
    > Constructor
    > Destructor
    > Destructor
    > Destructor
    >
    > I don't know if I am missing something. I checked all the references
    > that I have for exceptions. Looks like local objects get deleted
    > normally when exceptions are thrown. I cant seem to explain this
    > output. Would be grateful if the members here can help me with this.


    Your 'A' class has a copy constructor which you forgot to consider.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 20, 2007
    #2
    1. Advertising

  3. >>
    >>I don't know if I am missing something. I checked all the references
    >>that I have for exceptions. Looks like local objects get deleted
    >>normally when exceptions are thrown. I cant seem to explain this
    >>output. Would be grateful if the members here can help me with this.

    >
    >
    > Your 'A' class has a copy constructor which you forgot to consider.
    >
    > V


    It's just incredible how many times we get this post.

    john
     
    John Harrison, Feb 20, 2007
    #3
  4. Piyo Guest

    Piyo, Feb 20, 2007
    #4
  5. John Harrison wrote:
    >>> I don't know if I am missing something. I checked all the references
    >>> that I have for exceptions. Looks like local objects get deleted
    >>> normally when exceptions are thrown. I cant seem to explain this
    >>> output. Would be grateful if the members here can help me with this.

    >>
    >>
    >> Your 'A' class has a copy constructor which you forgot to consider.
    >>
    >> V

    >
    > It's just incredible how many times we get this post.


    If it's so frequently asked (which I can't vouch for), then perhaps
    somebody <wink-wink> could write up an entry for Marshall Cline to
    add to the FAQ Lite...

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 20, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ook
    Replies:
    10
    Views:
    576
  2. BeautifulMind
    Replies:
    7
    Views:
    679
    Ron Natalie
    Feb 8, 2007
  3. Replies:
    8
    Views:
    399
    James Kanze
    Mar 2, 2009
  4. Keith H Duggar
    Replies:
    5
    Views:
    698
    Bo Persson
    Jun 18, 2009
  5. Victor Bazarov
    Replies:
    11
    Views:
    767
    James Kanze
    Dec 9, 2009
Loading...

Share This Page