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: 
The fact that you are throwing an exception is irrelevant here. In your first example, `sd` is an object that exists on the stack. When execution exits its scope, for whatever reason, it gets destroyed. In the second example, `sd` is a pointer to an object that was explicitly allocated using `new`. This object will not be destroyed until that pointer is passed to `delete`; since you never do so, your program is currently leaking it.
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.