Difference between revisions of "On"
From Lazarus wiki
Jump to navigationJump to search (Created page with "{{On}} The <syntaxhighlight lang="pascal" enclose="none">on</syntaxhighlight> keyword to act on the exception type. The <syntaxhighlight lang="pasc...") |
|||
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
{{On}} | {{On}} | ||
− | The <syntaxhighlight lang="pascal" | + | |
− | The <syntaxhighlight lang="pascal" | + | Back to [[Reserved words]]. |
+ | |||
+ | |||
+ | The <syntaxhighlight lang="pascal" inline>on</syntaxhighlight> [[Keyword|keyword]] to act on the [[Exceptions|exception]] type. | ||
+ | The <syntaxhighlight lang="pascal" inline>on</syntaxhighlight> clause checks against one of a number of exception classes. | ||
<syntaxhighlight lang="pascal"> | <syntaxhighlight lang="pascal"> | ||
Line 16: | Line 20: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
Line 52: | Line 55: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | == | + | == See also == |
* [[Try]] | * [[Try]] | ||
* [[Except]] | * [[Except]] |
Latest revision as of 17:26, 6 August 2022
│
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.