Exceptions: Difference between revisions

Add page to "Flow control" category.
(Applesoft BASIC)
(Add page to "Flow control" category.)
 
(15 intermediate revisions by 9 users not shown)
Line 4:
{{omit from|M4}}
{{omit from|Retro}}
[[Category:Flow control]]
 
This task is to give an example of an exception handling routine
Line 20 ⟶ 21:
 
X.try
X.throw SillyError(‘egg’)
X.catch SillyError se
print(se.message)</syntaxhighlight>
Line 485 ⟶ 486:
}</syntaxhighlight>
 
=={{header|BBC BASIC}}==
==={{header|BBC BASIC}}===
<syntaxhighlight lang="bbcbasic"> ON ERROR PROCerror(ERR, REPORT$) : END
Line 1,044 ⟶ 1,046:
{
constructor new()
<= super new("MyException raised");
}</syntaxhighlight>
 
Line 1,057 ⟶ 1,059:
<syntaxhighlight lang="elena">try
{
o.foo()
}
catch:(MyException e)
{
// handle exceptions of type MyException and derived
Line 1,065 ⟶ 1,067:
 
'''Catching any exception'''
<syntaxhighlight lang="elena">o.foo() | on:(e) try
{
foo.fail()
};
catch(Exception e)
{
// handle any type of exception
Line 1,593 ⟶ 1,599:
Exceptions in langur are hashes guaranteed to contain certain fields, even if they're empty.
 
<syntaxhighlight lang="langur">#throw do"not somethinga math exception"
throw "not a math exception"
 
catch [.e] {
if .e["'cat"] == "math" {
# change result...
} else {
Line 1,605 ⟶ 1,610:
} else {
# no exception
}
...
}</syntaxhighlight>
 
An else section on a catch is optional. As of 0.7, youYou can also use else if on a catch.
 
=== shortenedexception catchvariable ===
An exception variable may be specified, or you can simply use the implicit variable, which is _err.
A catch can be shortened by using a single expression that does not start with a variable name. This uses the implicit _err exception variable. Prior to 0.7, the implicit exception variable was .err.
 
A shortened catch does not allow an else section (action for no exception).
 
<syntaxhighlight lang="langur">100 / 0
 
catch if _err["cat"] == "math" {
#if change_err'cat result== "math" {
# change result
123
} else {
throw} else {
# rethrow the exception
}</syntaxhighlight>
throw
...}
}
}</syntaxhighlight>
 
<syntaxhighlight lang="langur">val .safediv = ffn(.x, .y) { .x / .y ; catch : 0 }
.safediv(7, 7) # 1
.safediv(7, 0) # 0</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Lasso}}==
Line 2,819 ⟶ 2,827:
see 1/0
Catch
raise("Sorry we can't divide 1/0" + nl)
Done
</syntaxhighlight>
 
=={{header|RPL}}==
Basic RPL allows a conditional branching on error:
≪ '''IFERR''' '1/0' EVAL '''THEN'''
<span style="color:red">"Can't divide "</span> ROT →STR + <span style="color:red">" by "</span> + SWAP →STR + '''END'''
 
=={{header|Ruby}}==
Line 3,083 ⟶ 3,097:
An exception is thrown by the ''die'' keyword, which, if not caught, it terminates the program with an appropriate exit code.
<syntaxhighlight lang="ruby">try {
die "I'm dead!"; # throws an exception of type 'error'
}
catch { |type, msg|
say "msg: #{msg}"; # msg: I'm dead! at test.sf line 2.
}
catch { |type, msg|
say "type: #{type}"; # type: error
say "msg: #{msg}"; # msg: I'm dead! at test.sf line 2.
};
 
say "I'm alive...";
die "Now I'm dead!"; # this line terminates the program
say "Or am I?"; # Yes, you are!</syntaxhighlight>
{{out}}
<pre>
Line 3,098 ⟶ 3,111:
msg: I'm dead! at test.sf line 2.
I'm alive...
Now I'm dead! at test.sf line 109.
</pre>
 
Line 3,525 ⟶ 3,538:
 
Here's an example of all this.
<syntaxhighlight lang="ecmascriptwren">var intDiv = Fn.new { |a, b|
if (!(a is Num && a.isInteger) || !(b is Num && b.isInteger)) Fiber.abort("Invalid argument(s).")
if (b == 0) Fiber.abort("Division by zero error.")
Line 3,552 ⟶ 3,565:
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}}==
6

edits