Sign in Join
Wiki copytemporary

On: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: On

OldNewLine
1+Back to [[Reserved words]].
2+
3+The `on` [[Keyword|keyword]] to act on the [[Exceptions|exception]] type. The `on` clause checks against one of a number of exception classes.
4+
5+``` pascal
6+
7+try
8+ //guarded block of code
9+ // ...
10+except
11+ on {exception type} do begin
12+ //exception block-handles SomeException
13+ end;
14+end;
15+
16+```
17+
18+``` pascal
19+
20+program range_error;
21+{$mode objfpc} {$H+}
22+{$R+} // Range check on
23+uses sysutils;
24+var
25+ i,j:integer;
26+ arr:array[0..9] of integer;
27+begin
28+ try
29+ i := 0;
30+ j := 10;
31+ while i < j do
32+ begin
33+ inc(i);
34+ arr[i] := i + j;
35+ WriteLn( i,' ', arr[i]);
36+ end;
37+ except
38+ on E:ERangeError do begin
39+ WriteLn('Error: valid range exceeded detected.');
40+ WriteLn(E.Message);
41+ end;
42+ on E:Exception do // generic handler
43+ WriteLn('Caught ' + E.ClassName + ': ' + E.Message);
44+ end;
45+ WriteLn('Press any key');
46+ ReadLn;
47+end.
48+```
49+
50+## See also
51+
52+- [[Try]]
53+- [[Except]]
54+
55+---
56+*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](/wiki-contact).*

Back to history