Defer Panic and Recover
CONCEPTS:Go Defer Panic and Recover
Question Variations
- "When do deferred calls run, and in what order?"
- "Should a Go API use panic for expected failures?"
- "Why does `recover` not catch a panic from another goroutine?"
- "What value does a deferred closure observe?"
Why This Is Asked
This topic tests whether you can distinguish ordinary error handling from exceptional failure and reliably clean up resources. It also exposes misconceptions about defer timing and the limited scope in which a panic can be recovered.
Key Concepts
- Deferred calls run when the surrounding function returns, in last-in-first-out order.
- Arguments to a deferred call are evaluated when
deferexecutes. - A panic unwinds the current goroutine’s stack while running deferred calls.
recoveronly works when directly invoked by a deferred function in the panicking goroutine.
Question Variations
- “When do deferred calls run, and in what order?”
- “Should a Go API use panic for expected failures?”
- “Why does
recovernot catch a panic from another goroutine?” - “What value does a deferred closure observe?”