andowero
andowero

Reputation: 499

Exception in finally block

If exception is raised in finally block of code, is the rest of finally block executed, or not?

try
  statementList1
finally
  command;
  command_that_raises;
  critical_command;
end

Will critical_command be executed?

Manual talks only about exceptions, not execution of code:

If an exception is raised but not handled in the finally clause, that exception is propagated out of the try...finally statement, and any exception already raised in the try clause is lost. The finally clause should therefore handle all locally raised exceptions, so as not to disturb propagation of other exceptions.

Upvotes: 0

Views: 915

Answers (1)

Nasreddine Galfout
Nasreddine Galfout

Reputation: 2591

See the following confirmation:

procedure TForm6.Button1Click(Sender: TObject);
begin

  try
    ShowMessage('begin');
  finally
    ShowMessage('enter');
    raise Exception.Create('raise');
    ShowMessage('end');
  end;

end;

And now for this case:

procedure RaiseAndContinue;
begin
  try
    raise Exception.Create('raise');
  except

  end;
end;

procedure TForm6.Button1Click(Sender: TObject);
begin

  try
    ShowMessage('begin');
  finally
    ShowMessage('enter');
    RaiseAndContinue;
    ShowMessage('end');
  end;

end;

The short answer is: unless you handle that exception then No the code will not be executed.

Upvotes: 3

Related Questions