O'Reilly logo

Programming Elixir by Dave Thomas

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 rescue

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required