0
0
Goprogramming~20 mins

Recover usage in Go - Practice Problems & Coding Challenges

Choose your learning style9 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.