Sign in Join
Wiki copytemporary

Revision 1 of 1, by Kai Burghardt on 8-6-2022. The page as it is now.

On

Back to Reserved words.

The on keyword to act on the exception type. The on clause checks against one of a number of exception classes.

pascal

try
    //guarded block of code
    // ...
except
   on {exception type} do begin
     //exception block-handles SomeException
   end;
end;
pascal

program range_error;
{$mode objfpc} {$H+}
{$R+} // Range check on
uses sysutils;
var
  i,j:integer;
  arr:array[0..9] of integer;
begin
  try
    i := 0;
    j := 10;
    while i < j do
      begin
        inc(i);
        arr[i] := i + j;
        WriteLn( i,'  ', arr[i]);
      end;
  except
    on E:ERangeError do begin
      WriteLn('Error: valid range exceeded detected.');
      WriteLn(E.Message);
    end;
    on E:Exception do  // generic handler
      WriteLn('Caught ' + E.ClassName + ': ' + E.Message);
  end;
  WriteLn('Press any key');
  ReadLn;
end.

See also


From the Free Pascal wiki, page "On", as it stood on 2022-08-06 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.