What is the difference between ending a try-finally block with a try-except block and vice versa?

Is there any practical difference between the two coding patterns in Delphi:

Version 1

try
  try
    {Do something}
  finally
    {Do tidy up}
  end
except
  {Handle exception}
end;

Version 2

try
  try
    {Do something}
  except
    {Handle exception}
  end
finally
  {Do tidy up}
end;
+5
source share
2 answers

There are two differences:

  • The relative order in which exceptions and block endings are executed. In version 1, it is finally executed before the exception. In version 2, the exception order is canceled.
  • In version 1, if the finally block is raised, then it will be processed by the except block. In version 2, if the finally block is raised, then it will be processed by the next one containing the exception handler, that is, outside this code.

, . , , , -, , .

, , , , . , .

+6

try..except .

Resource := TAbstractResource.Create;
try
  Resource.DoSomeThing;
except
  On E:Exception Do 
   HandleException(E);
end;
FreeAndNil(Resource);
+2

All Articles