Trying, Rescuing, and Catching
With some functions, you don’t control the code—and it can raise errors or throw values. You need the try statement to handle the unexpected results. If you came from object-oriented languages like C++, Java, and Ruby, this technique will be familiar to you.
Most of the time you can easily identify the functions that can raise errors or throw values because their names end with an exclamation point. For example, the File.cd!/1 function raises an exception when the path doesn’t exist.
try wraps a code block. If an error is raised, you can use rescue to recover. An error (or exception) in Elixir is a special data structure that describes when an exceptional thing happens in the code. You can also use try to capture ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access