Exceptions: Difference between revisions
Content added Content deleted
Alextretyak (talk | contribs) m (→{{header|11l}}: X.throw) |
Langurmonkey (talk | contribs) |
||
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"> |
<syntaxhighlight lang="langur">throw "not a math exception" |
||
throw "not a math exception" |
|||
catch |
catch[.e] { |
||
if .e |
if .e'cat == "math" { |
||
# change result... |
# change result... |
||
} else { |
} else { |
||
Line 1,610: | Line 1,609: | ||
} else { |
} else { |
||
# no exception |
# no exception |
||
} |
|||
... |
|||
</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 |
if _err'cat == "math" { |
||
# change result |
# change result |
||
123 |
123 |
||
} else { |
} else { |
||
# rethrow the exception |
|||
throw |
throw |
||
} |
} |
||
} |
|||
</syntaxhighlight> |
|||
<syntaxhighlight lang="langur">val .safediv = |
<syntaxhighlight lang="langur">val .safediv = fn(.x, .y) { .x / .y ; catch : 0 } |
||
.safediv(7, 7) # 1 |
.safediv(7, 7) # 1 |
||
.safediv(7, 0) # 0 |
.safediv(7, 0) # 0 |
||
</syntaxhighlight> |
|||
=={{header|Lasso}}== |
=={{header|Lasso}}== |