Panic in Elixir
In Elixir, we use exceptions to handle unexpected errors or situations that shouldn’t occur during normal operation. This is similar to the concept of panic
in other languages.
Running this program will cause it to raise an exception, print an error message and stacktrace, and exit with a non-zero status.
When the first raise
in main
is executed, the program exits without reaching the rest of the code. If you’d like to see the program try to create a temp file, comment out the first raise
.
Note that in Elixir, it’s idiomatic to use pattern matching and the {:ok, result}
/ {:error, reason}
tuple convention for handling many errors, rather than raising exceptions for every possible error condition. Exceptions are typically used for truly exceptional situations that prevent the normal flow of the program.