Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the statements in the Finally block still execute in this piece of code ?

Will finally block execute? if I pass exit; ?

procedure someProc;
begin
    Try
      Exit;
    finally
     do_something;
    end;
end;
like image 624
jmp Avatar asked Dec 23 '11 01:12

jmp


4 Answers

Yes, finally blocks always execute, even if you call Exit somewhere. They wouldn't be worth much if they weren't always executed.

like image 61
Seth Carnegie Avatar answered Nov 04 '22 15:11

Seth Carnegie


The finally clause will always be executed, unless the executing thread enters a non-terminating loop, blocks indefinitely or is terminated abnormally, whilst executing the try clause.

The pertinent documentation states (emphasis mine):

The syntax of a try...finally statement is

try 
  statementList1
finally
  statementList2 
end 

where each statementList is a sequence of statements delimited by semicolons.

The try...finally statement executes the statements in statementList1 (the try clause). If statementList1 finishes without raising exceptions, statementList2 (the finally clause) is executed. If an exception is raised during execution of statementList1, control is transferred to statementList2; once statementList2 finishes executing, the exception is re-raised. If a call to the Exit, Break, or Continue procedure causes control to leave statementList1, statementList2 is automatically executed. Thus the finally clause is always executed, regardless of how the try clause terminates.

like image 39
David Heffernan Avatar answered Nov 04 '22 15:11

David Heffernan


A quick test app could have answered this question really quickly.

program TestFinally;

{$APPTYPE CONSOLE}

uses
  SysUtils;

begin
  try
    WriteLn('Before exiting');
    Exit;
  finally
    WriteLine('In finally. If you see this, it was written after "Exit" was called');
    ReadLn;
  end;
end.
like image 26
Ken White Avatar answered Nov 04 '22 15:11

Ken White


For the sake of completeness - finally block will not execute if the process or thread executing the try..finally block is terminated with TerminateProcess/TerminateThread.

For example, finally block will not be executed in the code below.

o := TObject.Create;
try
  TerminateThread(GetCurrentThread, 0);
finally
  o.Free;
end;
like image 32
gabr Avatar answered Nov 04 '22 16:11

gabr