Exceptions: Difference between revisions
Add page to "Flow control" category.
(→RPL: Add RPL) |
Flexibeast (talk | contribs) (Add page to "Flow control" category.) |
||
(10 intermediate revisions by 6 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 1,045 ⟶ 1,046:
{
constructor new()
<= super new("MyException raised");
}</syntaxhighlight>
Line 1,058 ⟶ 1,059:
<syntaxhighlight lang="elena">try
{
}
catch
{
// handle exceptions of type MyException and derived
Line 1,066 ⟶ 1,067:
'''Catching any exception'''
<syntaxhighlight lang="elena">
{
foo.fail()
catch(Exception e)
{
// handle any type of exception
Line 1,594 ⟶ 1,599:
Exceptions in langur are hashes guaranteed to contain certain fields, even if they're empty.
<syntaxhighlight lang="langur">
catch
if .e
# change result...
} else {
Line 1,606 ⟶ 1,610:
} else {
# no exception
}
An else section on a catch is optional.
=== exception variable ===
An exception variable may be specified, or you can simply use the implicit variable, which is _err
<syntaxhighlight lang="langur">100 / 0
catch {
if _err
# change result
123
} else {
# rethrow the exception
throw
}
}
<syntaxhighlight lang="langur">val .safediv =
.safediv(7, 7) # 1
.safediv(7, 0) # 0
</syntaxhighlight>
=={{header|Lasso}}==
Line 2,826 ⟶ 2,833:
=={{header|RPL}}==
Basic RPL allows a conditional branching on error:
≪
<span style="color:red">"Can't divide "</span> ROT →STR + <span style="color:red">" by "</span> + SWAP →STR +
≫
Line 3,090 ⟶ 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!"
}
}
▲catch { |type, msg|
▲ say "msg: #{msg}"; # msg: I'm dead! at test.sf line 2.
▲};
say "I'm alive..."
die "Now I'm dead!"
say "Or am I?"
{{out}}
<pre>
Line 3,105 ⟶ 3,111:
msg: I'm dead! at test.sf line 2.
I'm alive...
Now I'm dead! at test.sf line
</pre>
Line 3,532 ⟶ 3,538:
Here's an example of all this.
<syntaxhighlight lang="
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.")
|