On

From Free Pascal wiki
Revision as of 15:20, 3 April 2019 by Djzepi (talk | contribs) (Created page with "{{On}} The <syntaxhighlight lang="pascal" enclose="none">on</syntaxhighlight> keyword to act on the exception type. The <syntaxhighlight lang="pasc...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

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

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