Search⌘ K
AI Features

Exceptions

Explore how to raise exceptions properly in Elixir and understand their role in control flow. This lesson teaches you how to handle errors in file operations effectively, differentiate when to raise exceptions, and use Elixir conventions like File.open! for meaningful error reporting.

Raising exceptions

First, the official warning: exceptions in Elixir are not control-flow structures. Instead, Elixir exceptions are intended for things that should never happen in normal operation. That means the database going down or a name server failing to respond could be considered exceptional. Failing to open a configuration file whose name is fixed could be seen as exceptional. However, failing to open a file whose name a user entered is not.

We raise an exception with the raise function. At its simplest level, we pass it a string, and it generates an ...