On deletion of exception objects in C++

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,


I am facing an issue regarding the exception handling mechanism in C++.

I created an exception object on the heap and threw it using a
reference. When I tried to delete the object, the program crashed. The
program is reproduced below:

-----------------------------------------------------------
#include <iostream>
using namespace std;

class E
{
};

class A
{
    public:
        void f() throw (E&)
        {
            E* e = new E();
            E& x = (*e);
            throw x;

        }
};
class B
{
    A a;
    public:
        void f() throw (E&)
        {
            try
            {
                a.f();
            }
            catch (E& e)
            {
                throw e;
            }
        }
};

int main()
{
    B b;

    try
    {
        b.f();
    }
    catch (E& e)
    {
        delete (&e);
    }
}

---------------------------------------------------

You can save it as "testException.cpp", compile and run it as:
g++ testException.cpp
./a.out

Could you please help me understand the issue?

Thanks in advance.

Regards,
Debarshi

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux