Bird
Raised Fist0
Goprogramming~20 mins

Recover usage in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Go Recover Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Go program using recover?

Consider the following Go code that uses recover() inside a deferred function. What will it print?

Go
package main

import "fmt"

func main() {
    defer func() {
        if r := recover(); r != nil {
            fmt.Println("Recovered from panic:", r)
        }
    }()
    fmt.Println("Start")
    panic("something went wrong")
    fmt.Println("End")
}
AStart\nEnd
BStart\nEnd\nRecovered from panic: something went wrong
CStart\nRecovered from panic: something went wrong
DStart\npanic: something went wrong
Attempts:
2 left
💡 Hint

Remember that defer runs after the panic, and recover() stops the panic.

❓ Predict Output
intermediate
2:00remaining
What happens if recover is called outside deferred function?

What will this Go program print?

Go
package main

import "fmt"

func main() {
    r := recover()
    fmt.Println("Recover returned:", r)
    panic("panic here")
}
ARecover returned: <nil>\npanic: panic here
BRecover returned: panic here
CRecover returned: <nil>
DCompile error: recover used outside deferred function
Attempts:
2 left
💡 Hint

recover() only works inside deferred functions during a panic.

🔧 Debug
advanced
2:00remaining
Why does this recover not catch the panic?

Look at this Go code. It tries to recover from a panic but fails. Why?

Go
package main

import "fmt"

func main() {
    defer recoverPanic()
    panic("fail")
}

func recoverPanic() {
    if r := recover(); r != nil {
        fmt.Println("Recovered:", r)
    }
}
AThe recover call is in a separate function, so it cannot catch the panic from main.
BThe deferred function is called after main returns, so recover can't catch the panic.
CThe recover call must be inside an anonymous deferred function to work.
DThe panic is not active when recover is called because defer runs too late.
Attempts:
2 left
💡 Hint

Think about where the panic happens and where recover is called.

📝 Syntax
advanced
2:00remaining
Which option correctly uses recover to handle panic?

Which of the following Go code snippets correctly uses recover() to handle a panic?

Adefer recover()
Bdefer func() { if r := recover(); r != nil { fmt.Println(r) } }()
Cfunc handle() { r := recover(); if r != nil { fmt.Println(r) } } defer handle()
Ddefer func() { recover() }()
Attempts:
2 left
💡 Hint

Recover must be called inside a deferred function to catch panics.

🚀 Application
expert
3:00remaining
How many times will deferred recover run in nested panics?

Consider this Go program with nested panics and deferred recover calls. How many times will the recover print a message?

Go
package main

import "fmt"

func main() {
    defer func() {
        if r := recover(); r != nil {
            fmt.Println("Recovered in main:", r)
        }
    }()

    func() {
        defer func() {
            if r := recover(); r != nil {
                fmt.Println("Recovered in inner func:", r)
                panic("new panic")
            }
        }()
        panic("original panic")
    }()
}
A1 time: only in inner func
B0 times: recover does not catch panics here
C1 time: only in main
D2 times: once in inner func and once in main
Attempts:
2 left
💡 Hint

Think about how recover stops a panic and how a new panic inside a deferred function affects outer deferred functions.

Practice

(1/5)
1. What is the main purpose of the recover function in Go?
easy
A. To handle errors returned by functions
B. To start a new goroutine
C. To pause the execution of a function
D. To catch a panic and prevent the program from crashing

Solution

  1. Step 1: Understand panic and recover

    Panic causes a program to stop immediately unless recovered.
  2. Step 2: Role of recover

    Recover catches the panic inside a deferred function to keep the program running.
  3. Final Answer:

    To catch a panic and prevent the program from crashing -> Option D
  4. Quick Check:

    recover catches panic = A [OK]
Hint: Recover catches panics only inside deferred functions [OK]
Common Mistakes:
  • Thinking recover handles normal errors
  • Using recover outside deferred functions
  • Confusing recover with panic
2. Which of the following is the correct way to use recover in Go?
easy
A. Call recover directly inside the main function
B. Call recover inside a deferred function
C. Call recover inside a goroutine
D. Call recover after a panic has finished

Solution

  1. Step 1: Identify recover usage rules

    Recover must be called inside a deferred function to catch panics.
  2. Step 2: Analyze options

    Only Call recover inside a deferred function calls recover inside a deferred function.
  3. Final Answer:

    Call recover inside a deferred function -> Option B
  4. Quick Check:

    recover inside defer = B [OK]
Hint: Always use recover inside defer to catch panics safely [OK]
Common Mistakes:
  • Calling recover directly without defer
  • Calling recover after panic ends
  • Using recover in goroutines without defer
3. What will be the output of this Go program?
package main
import "fmt"
func main() {
    defer func() {
        if r := recover(); r != nil {
            fmt.Println("Recovered from panic:", r)
        }
    }()
    panic("something went wrong")
    fmt.Println("This will not print")
}
medium
A. Recovered from panic: something went wrong
B. something went wrong
C. This will not print
D. Program crashes with panic message

Solution

  1. Step 1: Understand defer and recover behavior

    The deferred function runs after panic starts, calling recover to catch it.
  2. Step 2: Output analysis

    Recover prints the panic message, so output is "Recovered from panic: something went wrong".
  3. Final Answer:

    Recovered from panic: something went wrong -> Option A
  4. Quick Check:

    defer + recover prints panic message = C [OK]
Hint: Recover prints panic message inside deferred function [OK]
Common Mistakes:
  • Expecting code after panic to run
  • Thinking panic message prints directly
  • Ignoring deferred recover function
4. What is wrong with this Go code snippet?
func main() {
    recover()
    panic("error")
}
medium
A. panic() cannot be called without defer
B. panic() must be called before recover()
C. recover() is called outside a deferred function and will not catch panic
D. recover() cannot be called in main function

Solution

  1. Step 1: Check recover usage

    Recover only works inside deferred functions to catch panics.
  2. Step 2: Analyze code

    Recover is called directly, so it won't catch the panic, causing program to crash.
  3. Final Answer:

    recover() is called outside a deferred function and will not catch panic -> Option C
  4. Quick Check:

    recover outside defer = no panic catch = D [OK]
Hint: Recover must be inside defer to catch panic [OK]
Common Mistakes:
  • Calling recover directly without defer
  • Assuming recover catches panic anytime
  • Misunderstanding panic and recover order
5. You want to safely handle panics in a function that divides two numbers and returns the result. Which code snippet correctly uses recover to avoid crashing when dividing by zero?
hard
A. func safeDivide(a, b int) int { defer func() { if r := recover(); r != nil { fmt.Println("Recovered from panic") } }() return a / b }
B. func safeDivide(a, b int) int { if b == 0 { recover() } return a / b }
C. func safeDivide(a, b int) int { return a / b defer recover() }
D. func safeDivide(a, b int) int { defer recover() return a / b }

Solution

  1. Step 1: Identify correct recover usage

    Recover must be called inside a deferred anonymous function to catch panics.
  2. Step 2: Analyze options

    Only func safeDivide(a, b int) int { defer func() { if r := recover(); r != nil { fmt.Println("Recovered from panic") } }() return a / b } uses defer with an anonymous function calling recover properly.
  3. Final Answer:

    func safeDivide(a, b int) int { defer func() { if r := recover(); r != nil { fmt.Println("Recovered from panic") } }() return a / b } -> Option A
  4. Quick Check:

    defer func with recover = safe panic handling = A [OK]
Hint: Use defer with anonymous func calling recover to catch panics [OK]
Common Mistakes:
  • Calling recover without defer
  • Calling recover outside anonymous function
  • Placing defer after return statement
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard