This text is a very short (and dirty) reference for the syntax of handling exceptions in the Erlang programming language. It is just a complementary text for the official reference.
Try
Evaluates the expressions between the try and the of and matches the result against the patterns placed before the catch just like in a case construction. If an exception is raised in one of these expressions, this is matched against the patterns after catch.
try exp1, exp2, ... of
Pattern -> ...
...
catch
error:Pattern -> ... (for erlang:error(Pattern))
throw:Pattern -> ... (for throw(Pattern))
exit:Pattern -> ... (for exit(Pattern))
...
error:{badmatch, Val} -> ... (for bad match runtime error
with: Pattern = Val)
...
Pattern -> ... (the default error class
if omitted is throw)
Pattern:Pattern -> ...
_:_ -> ...
end
|
Note
|
Between of and catch exceptions are not caught. |
Alternate try
In this case the alternate construction differs from the case form and no pattern matching is done against the evaluated expressions:
try
exp1,
exp2,
...
catch
...
end
After clause
The after clause permits clean up actions because no returned value is produced from it:
try
...
catch
...
after
... (does no return any value)
end
Old catch expression
The old way to deal with exceptions was the catch form, but now the try construction is preferred. Being much less sophisticated, it transforms the exception in a simple value with the next format depending of each case:
Val = catch(throw(Val))
{'EXIT', Reason} = catch(exit(Reason))
{'EXIT', {Reason, Stack}} = catch(erlang:error(Reason))
Sometimes it is needed a pair of parenthesis around the catch construct because of its awkward low operator precedence:
Val = (catch(X))