Let's say you have a piece of code like:
resource = allocateResource();
try { /* dangerous code here */ }
finally { free(resource); }
I'm not referring to any specific language here, but I guess Java, C#, and C++ would be good examples (assuming you're using __try
/__finally
in MSVC++).
Is this exception-safe?
Personally, I don't think this is exception-safe, because what if there's an exception before you enter the try
block? Then your resource will leak.
I've seen this enough times, though, that I think it I'm missing something... am I? Or is this really unsafe?
Edit:
I'm not asking about allocateResource
throwing an exception, but a situation in which you get an exception after that function has returned, but before resource
is assigned.