When an exception is thrown, the stack is unwound (running destructors of local objects) until a catch
stops the exception. There's nothing that automatically sets the pointer to NULL
, and it's unilkely that one of the destructors does so - it would be a contrived example that did.
Usually, there's no need to set the pointer to NULL
. In fact, often you can't. Take the following code:
try {
std::string s("hello, world");
} catch (std::bad_alloc) {
// ???
}
It's possible that the allocation of those 12 bytes fails. The exception will return from the constructor of std::string
. At that point, the object s
no longer exists, nor does the pointer s._SomeInternalName
inside s
. The compiler will therefor report an error if you try to use s
in the catch block. And if the pointer doesn't exist anymore, you obviously can't set it to 0.