Exceptions: Difference between revisions

Content added Content deleted
Line 1,598: Line 1,598:
Exceptions in langur are hashes guaranteed to contain certain fields, even if they're empty.
Exceptions in langur are hashes guaranteed to contain certain fields, even if they're empty.


<syntaxhighlight lang="langur"># do something
<syntaxhighlight lang="langur">throw "not a math exception"
throw "not a math exception"


catch .e {
catch[.e] {
if .e["cat"] == "math" {
if .e'cat == "math" {
# change result...
# change result...
} else {
} else {
Line 1,610: Line 1,609:
} else {
} else {
# no exception
# no exception
}
...
}</syntaxhighlight>
</syntaxhighlight>


An else section on a catch is optional. You can also use else if.
An else section on a catch is optional. You can also use else if.
Line 1,621: Line 1,620:


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


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


=={{header|Lasso}}==
=={{header|Lasso}}==