Let's have a piece of code (fstream is just an example, we could be talking about dynamic memory allocation...):
fstream f;
try {
f.open("xxx");
...
f.close();
} catch (...) {
...
}
When something goes wrong I would like to close() the file (release memory or whatever), but I don't know the state of f. After all, the exception may come from f.open(). I don't think it would be safe to call f.close() in the catch clause as I can no longer believe f.
f could also be a pointer to a dynamically allocated array which I would like to delete [], but who knows where it points to after the exception was thrown...
This may not be very common, but what can I do when I absolutely can't affort any additional damage?
I can think about an immediate abort().
Thanks.