May 2018
Intermediate to advanced
412 pages
9h 3m
English
Elixir code (and the underlying Erlang libraries) can raise a second kind of error. These are generated when a process calls error, exit, or throw. All three take a parameter, which is available to the catch handler.
Here’s an example:
| | defmodule Catch do |
| | |
| | def start(n) do |
| | try do |
| | incite(n) |
| | catch |
| | :exit, code -> "Exited with code #{inspect code}" |
| | :throw, value -> "throw called with #{inspect value}" |
| | what, value -> "Caught #{inspect what} with #{inspect value}" |
| | end |
| | end |
| | |
| | |
| | defp incite(1) do |
| | exit(:something_bad_happened) |
| | end |
| | |
| | defp incite(2) do |
| | throw {:animal, "wombat"} |
| | end |
| | |
| | defp incite(3) ... |
Read now
Unlock full access