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 ...