Why is a local automatic object from a try block still alive in the catch block when I throw that object by address? [duplicate]

Solution 1:

Why is object a1 still alive in the catch block

It's no longer alive.

you can check it for yourself, print method works

That proves nothing about the lifetime of the object.

I thought that all local objects in the try block get destroyed as soon as we leave it?

You have been thinking correctly.


The behaviour of the program is undefined.

I expected my program to crash

This is where you made the biggest mistake. The program is not guaranteed to crash when the behaviour is undefined. You cannot rely on such assumption.