Raising an Exception

You can raise an exception using the raise function. At its simplest, you pass it a string and it generates an exception of type RuntimeError.

 iex>​ ​raise​ ​"​​Giving up"
 **​ (RuntimeError) Giving up
  erl_eval.erl:572: :erl_eval.do_apply/6

You can also pass the type of the exception, along with other optional fields. All exceptions implement at least the message field.

 iex>​ ​raise​ RuntimeError
 **​ (RuntimeError) runtime error
  erl_eval.erl:572: :erl_eval.do_apply/6
 iex>​ ​raise​ RuntimeError, ​message:​ ​"​​override message"
 **​ (RuntimeError) override message
  erl_eval.erl:572: :erl_eval.do_apply/6

You can intercept exceptions using the try function. It takes a block of code to execute, and optional ...

Get Programming Elixir ≥ 1.6 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.