Exception Handling in Delphi - ablealias/Delphi GitHub Wiki

Exception allows you to interrupt a program’s normal flow of control. You can raise an exception in any function, procedure or method. Delphi has two statements for dealing with exceptions, the Try-Except-End and Try-Finally-End statements. The Try-Except-End statement set up an exception handler that gets control when something goes wrong. The Try-Finally-End statement does not handle exception explicitly, but guarantees that the code in the finally part of the statement always run, even if an exception is raised. Use Try-Except to deal with errors. Use Try-Finally when you have a resource that must be cleaned up properly, no matter what happens. Each Try-Except statement can declare many on sections, where each section declares an exception class.

When your code raise an exception, it must pass a object to the raise statement. Usually, a program creates a new exception object as part of the raise statement, but in rare case you might want to raise an object that already exists, ie, EDivByZero. While raise an exception, delphi searches the call stack to find try statements. When it finds a try-finally, it executes the code in the finally part of the statement, then continues to search the stack of an exception handler. When the stack finds a try-except block, Delphi searches the on sections, delphi runs the code in the except part of the statement. If there are on sections, Delphi tries to find a match, or it runs the code in the else part of the except block. The variable that is declared in the on statement contains a reference to the exception object. Delphi automatically frees the exception object after the exception handler finishes. If delphi reaches the end of the call stack without finding a matching exception handler, it calls ExceptProc function pointer. If a finally block raise an exception, the old exception object is freed, and Delphi handles a new exception. If you have an Exit key word in the try block of a try-finally statement, delphi should runs the code in the finally part of the statement. If you have an Abort key word in the try block of try-except block, delphi runs the code in the except part of the statement. An Abort function is a silent exception.

procedure TForm1.Button1Click(Sender: TObject);
var
  Obj: TSomeObject;
begin
  try
    Screen.Cursor:= crHourGlass;

    Obj:= TSomeObject.Create;
    try
      // do something
    finally
      Obj.Free;
    end;

    Screen.Cursor:= crDefault;
  except on E: Exception do
  begin
    Obj.Free;
    Screen.Cursor:= crDefault;
    ShowMessage('There was an error: ' + E.Message);
  end;
end;

Division by Zero Exception Handling Example,

var
  number, zero : Integer;
begin
  // Try to divide an integer by zero - to raise an exception
  number := -1;
  try
    try
      zero   := 0;
      number := 1 div zero;
      ShowMessage('number / zero = '+IntToStr(number));
    finally
      if number = -1 then
      begin
        ShowMessage('Number was not assigned a value - using default');
        number := 0;
      end;
    end;
   except
      on E : Exception do
        ShowMessage(E.ClassName+' error raised, with message : '+E.Message);
   end;
end;