Erlang / Erlang Advanced Interview questions
Explain the internal working of exception propagation through nested try/catch blocks?
An exception raised anywhere inside a try block unwinds the call stack looking for the
nearest enclosing catch whose pattern matches the exception's class and reason — skipping
over any intervening function calls that don't themselves catch it, exactly like exception handling in most
languages.
flowchart TD
A[inner() raises error:bad_input] --> B{Caught by inner try/catch?}
B -->|No matching clause| C[Propagates up through middle/]
C --> D{Caught by middle's try/catch?}
D -->|No matching clause| E[Propagates up through outer/]
E --> F{Caught by outer's try/catch?}
F -->|Yes| G[Handled here]
If no enclosing try/catch anywhere up the call chain matches, the exception reaches the top of
the process and terminates it, which then behaves exactly like any other crash — propagating as an exit
signal to linked processes. A nested try only intercepts exceptions raised within its own body;
it has no effect on exceptions from calls made before it or after it returns, which is why the innermost
matching handler along the call chain is the one that actually catches it, not necessarily the outermost.
More Related questions...