Question: I'm trying to understand the behavior of exceptions in c++.
I wrote the following code:

```
class A{
public: 
    A(){
    };
    ~A(){
        cout<<"hello";

    };
};
int exceptionTest(){

    throw "blablabla";
};

int main(){

        A sd;
    int test = exceptionTest();
     return 0;
}

```

I've noticed that in this case the distructor gets called even though no one caught the exception. 
If I change the "main" code to:

```
 int main(){

            A* sd = new A();
        int test = exceptionTest();
         return 0;
    }

```

The distructor will not be called.
Can anyone please tell me what is the reason for the different behavior?

Thanks,
Li

Answer: 
Local variable destructors are called automatically, as soon as the variable is out of scope.
Destructors are never called on pointers, so you must call it yourself.
This is not really an answer, but I might clarify the behavior, in case of RAII mechanism, that I understood from the other answer and Mike's comments.

```
#include <iostream>

class Bar
{
        public:
        Bar() { std::cout << "Bar constructor" << std::endl; }
        ~Bar() { std::cout << "Bar destructor" << std::endl; }
};

void foo()
{
        throw("Exception");
}

int main()
{
        // Variation, add { to create a new scope
        Bar bar;
        foo();
        // Variation : }
        return 0;
}

```

Using `g++`, this code, where the exception is not catched will output the following:

```
Bar constructor
terminate called after throwing an instance of 'char const*'
Aborted

```

Meaning that `g++` does not unwind the stack (or let go the variable out of scope, if I understand the "variant" correctly), so the destructor is not called.

However, if you catch the exception:

```
#include <iostream>

class Bar
{
        public:
        Bar() { std::cout << "Bar constructor" << std::endl; }
        ~Bar() { std::cout << "Bar destructor" << std::endl; }
};

void foo()
{
        throw("Exception");
}

int main()
{
        try
        {
                Bar bar;
                foo();
        }
        catch (...)
        {
                // Nothing here
        }
        return 0;
}

```

then the output will be

```
Bar constructor
Bar destructor

```

and you recover the correct behavior.