Delphi Exception Handling - How to clean up properly?

Posted by Tom on Stack Overflow See other posts from Stack Overflow or by Tom
Published on 2010-03-18T21:51:35Z Indexed on 2010/03/18 23:01 UTC
Read the original article Hit count: 954

Filed under:
|

I'm looking at some code in an application of ours and came across something a little odd from what I normally do. With exception handling and cleanup, we (as well as many other programmers out there, I'm sure) use a Try/Finally block embedded with a Try/Except block. Now I'm used to the Try/Except inside the Try/Finally like so:

Try
  Try
    CouldCauseError(X);
  Except
    HandleError;
  end;
Finally
  FreeAndNil(x);
end;

but this other block of code is reversed as so:

Try
  Try
    CouldCauseError(X);
  Finally
    FreeAndNil(x);
  end;
Except
  HandleError;
end;

Looking around the web, I'm seeing folks doing this both ways, with no explanation as to why. My question is, does it matter which gets the outside block and which gets the inside block? Or will the except and finally sections get handled no matter which way it is structured? Thanks.

© Stack Overflow or respective owner

Related posts about delphi

Related posts about exception-handling