Exceptions: Difference between revisions

Added XPL0 example.
(BBC BASIC moved to the BASIC section.)
(Added XPL0 example.)
Line 3,553:
Caught Num does not implement 'badMethod()'.
</pre>
 
=={{header|XPL0}}==
XPL0 does not have the commands throw, try and catch; nor their equivalents.
 
By default, exceptions such as divide-by-zero cause a program to abort.
That behavior is called error trapping. Robust programs disable error
trapping by passing an integer bit array to a built-in Trap intrinsic
that selectively disables about a dozen error traps (by setting their
corresponding bits to 0). The traps are often all disabled by simply
passing 'false' when a section of code is about to do something that
might cause an exception, such as accessing a disk file that might not be
available. In this case the intrinsic GetErr is called, which returns a
value indicating which error occurred; and if an error is detected, the
program can take appropriate action without simply aborting.
 
Generally, aborting on errors is suitable during the debugging stage, but
often it's the worst alternative for an end user.
 
A related intrinsic, Restart, is useful if an error is detected in a
procedure nested many levels deep. Instead of returning the error
indication up through the many levels, it can be easier to restart the
program. A call to the Rerun intrinsic distinguishes a restart from a
normal start, and that can be used to properly handle the error.
<syntaxhighlight lang "XPL0">
real A;
[Trap(false);
A:= Sqrt(-42.);
if GetErr then
Text(0, "Square root of a negative value.
");
RlOut(0, A);
]</syntaxhighlight>
{{out}}
<pre>
Square root of a negative value.
NAN</pre>
 
=={{header|zkl}}==
295

edits