On

From Free Pascal wiki
Jump to navigationJump to search

Deutsch (de) English (en) suomi (fi)


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.

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


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