Here is the code:
#include <iostream>
using namespace std;
class A {
public:
void print() {
cout << "Object is still alive" << endl;
}
};
int main() {
try {
A a1 = A();
A* a = &a1;
throw a;
}
catch (A* a) {
a->print();
}
}
Why is object a1 still alive in the catch block (you can check it for yourself, print method works) even though I have thrown exception of type pointer to an object of class A? I thought that all local objects in the try block get destroyed as soon as we leave it?