This flow shows how defer and recover work together to catch and handle panics inside a function.
Execution Sample
Go
func safeDivide(a, b int) int {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered from panic:", r)
}
}()
return a / b
}
This function divides two numbers but recovers from a panic if division by zero happens.
Execution Table
Step
Action
Evaluation
Result
1
Call safeDivide(10, 2)
No panic
Returns 5
2
Call safeDivide(10, 0)
Panic: division by zero
recover() catches panic
3
Deferred function runs
recover() returns panic info
Prints 'Recovered from panic: runtime error: integer divide by zero'
4
safeDivide returns
No value returned explicitly after panic
Returns 0 (default int)
5
Program continues
No crash
Execution continues normally
💡 Execution stops normally after recover handles panic, preventing program crash.
Variable Tracker
Variable
Start
After Step 1
After Step 2
After Step 3
Final
a
10
10
10
10
10
b
2 or 0
2 or 0
2 or 0
2 or 0
2 or 0
r (recover)
nil
nil
panic info or nil
panic info or nil
nil after defer ends
Key Moments - 3 Insights
Why does the program not crash when dividing by zero inside safeDivide?
Because the deferred function calls recover(), which catches the panic and prevents the program from crashing, as shown in execution_table step 3.
What does recover() return if there is no panic?
recover() returns nil if no panic occurred, so the deferred function does nothing, as seen in execution_table step 1.
Why does safeDivide return 0 after a panic?
After a panic, the function does not reach the return statement normally, so it returns the zero value for int, which is 0, as shown in execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what does recover() return at step 3 when a panic occurs?
Anil
BThe result of division
CThe panic information (error message)
DA boolean true
💡 Hint
Check execution_table row 3 under 'Evaluation' and 'Result' columns.
At which step does the program print the recovered panic message?
AStep 1
BStep 3
CStep 2
DStep 5
💡 Hint
Look at execution_table row 3 under 'Result' for the print statement.
If recover() was not used, what would happen at step 2?
APanic would crash the program
BProgram would continue normally
CFunction would return 0
DPanic would be ignored
💡 Hint
Refer to the concept_flow and execution_table exit_note about panic handling.
Concept Snapshot
func safeDivide(a, b int) int {
defer func() {
if r := recover(); r != nil {
// handle panic
}
}()
return a / b
}
- Use defer + recover to catch panics
- recover returns panic info or nil
- Prevents program crash on runtime errors
Full Transcript
This example shows how Go's recover function works inside a deferred function to catch panics. When safeDivide is called with b=0, a panic occurs due to division by zero. The deferred anonymous function calls recover(), which returns the panic information. This stops the panic from crashing the program. The function then returns the zero value for int, which is 0. If no panic occurs, recover returns nil and the function returns the division result normally. This technique helps keep programs running safely even when unexpected errors happen.