Topic: Is this valid C++ code? (Throwing exception in destructor)


Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1998/09/22
Raw View
Siemel Naran wrote:

[...]

> The standard behaviour is to call abort() or teminate() -- not
> sure what's the difference -- to terminate the program abruptly
> when there are two exceptions in the picture.

The difference is that you can replace the function called by
terminate, however you cannot do that with abort. The standard
terminate behaviour - without set_terminate call - is to call
abort, so in that case, there is AFAIK no difference.

[...]
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: syd@quango.force9.co.uk
Date: 1998/09/18
Raw View
#include <stdio.h>

class Test
{
  public:
    Test()
    {
        puts( "In Test::Test()" );
    }
    ~Test()
    {
        puts( "In Test::~Test()" );
        throw 20;
    }
};

int main()
{
    try
    {
        try
        {
            Test t;
            throw 10;
        }
        catch ( int i )
        {
            printf( "i = %d\n", i );
        }
    }
    catch ( int j )
    {
        printf( "j = %d\n", j );
    }
    return 0;
}

Microsoft Visual C++ 5 SP3 when compiled using:

cl -W4 -GX test.cpp

produces the output

    In Test::Test()
    In Test::~Test()

    abnormal program termination

(This last line is what is displayed when an exception is not caught)

I had been expecting one of the following as output.  Which is correct?

    In Test::Test()
    In Test::~Test()
    i = 10

or

    In Test::Test()
    In Test::~Test()
    j = 20

Many thanks

Geoff


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: sbnaran@fermi.ceg.uiuc.edu (Siemel Naran)
Date: 1998/09/21
Raw View
On 18 Sep 1998 19:51:50 GMT, syd@quango.force9.co.uk

>    ~Test()
>    {
>        puts( "In Test::~Test()" );
>        throw 20;
>    }

An exception throwing dtor?

Suppose someone creates a Test object.
Suppose this someone throws an exception E1.
The local Test object created above must be destroyed.
So ~Test() is called and it throws an exception E2.
Now which exception do we 'return' to the caller -- E1 or E2?

One solution is to throw an exception pair<E1,E2>.
This idea makes programs tough to understand, and is difficult
to implement.  Specifically, how might you catch compound
exceptions?
The standard behaviour is to call abort() or teminate() -- not
sure what's the difference -- to terminate the program abruptly
when there are two exceptions in the picture.

(BTW, contrast exit() with abort().  exit() does destroy global
objects).


>Microsoft Visual C++ 5 SP3 when compiled using:

It is correct.


--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: huynh@anubisinc.com
Date: 1998/09/21
Raw View
In article <ubtodb6xq.fsf@quango.force9.co.uk>,
  syd@quango.force9.co.uk wrote:

> [code deleted]

> Microsoft Visual C++ 5 SP3 when compiled using:
>
> cl -W4 -GX test.cpp
>
> produces the output
>
>     In Test::Test()
>     In Test::~Test()
>
>     abnormal program termination

Yes, it is normal. Exceptions are not recursive. When they are thrown in
destructors called from stack unwielding, "unexpected" is called which aborts
the program unless it is overloaded.

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]