Exceptions: Difference between revisions

Content added Content deleted
(Applesoft BASIC)
(→‎{{header|langur}}: added curly braces to catch for clarity)
Line 1,610: Line 1,610:
An else section on a catch is optional. As of 0.7, you can also use else if on a catch.
An else section on a catch is optional. As of 0.7, you can also use else if on a catch.


=== shortened catch ===
=== exception variable ===
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.
An exception variable may be specified, or you can simply use the implicit variable, which is _err. 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
<syntaxhighlight lang="langur">100 / 0


catch if _err["cat"] == "math" {
catch {
if _err["cat"] == "math" {
# change result
# change result
123
123
} else {
throw
} else {
throw
}
}</syntaxhighlight>
}</syntaxhighlight>


<syntaxhighlight lang="langur">val .safediv = f { .x / .y ; catch 0 }
<syntaxhighlight lang="langur">val .safediv = f { .x / .y ; catch { 0 } }
.safediv(7, 7) # 1
.safediv(7, 7) # 1
.safediv(7, 0) # 0</syntaxhighlight>
.safediv(7, 0) # 0</syntaxhighlight>