Skip to content

Expand description of how catch works #13501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion lib/elixir/pages/getting-started/try-catch-and-rescue.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,28 @@ iex> try do
"not really"
```

Using `try/catch` is already uncommon and using it to catch exits is even rarer.
`catch` can also be used within a function body without a matching `try`.

```elixir
defmodule Example do
def matched_catch do
exit(:timeout)
catch
:exit, :timeout ->
{:error, :timeout}
end

def mismatched_catch do
exit(:timeout)
catch
# Since no clause matches, this catch will have no effect
:exit, :explosion ->
{:error, :explosion}
end
end
```

However, using `try/catch` is already uncommon and using it to catch exits is even rarer.

`exit` signals are an important part of the fault tolerant system provided by the Erlang VM. Processes usually run under supervision trees which are themselves processes that listen to `exit` signals from the supervised processes. Once an `exit` signal is received, the supervision strategy kicks in and the supervised process is restarted.

Expand Down