A panic is a built-in function that stops the normal execution of a program. It is used when something unexpected happens, like an error that can't be handled normally.
Click to reveal answer
beginner
How does Go handle a panic by default?
By default, when a panic occurs, Go stops the current function and starts unwinding the stack, running deferred functions along the way, and then the program crashes with a message.
Click to reveal answer
intermediate
What is the role of the recover function in panic behavior?
The recover function can catch a panic inside a deferred function, stopping the program from crashing and allowing the program to continue running.
Click to reveal answer
intermediate
When should you use panic in Go?
Use panic only for serious errors that should stop the program immediately, like bugs or unexpected states. For normal errors, use error values instead.
Click to reveal answer
beginner
What happens to deferred functions when a panic occurs?
Deferred functions still run when a panic happens. This lets you clean up resources or recover from the panic before the program stops.
Click to reveal answer
What does the panic function do in Go?
AStops normal execution and starts stack unwinding
BRecovers from an error
CHandles normal errors
DStarts a new goroutine
✗ Incorrect
panic stops normal execution and begins unwinding the stack.
Which function can catch a panic to prevent the program from crashing?
Adefer
Brecover
Cpanic
Dclose
✗ Incorrect
recover can catch a panic inside a deferred function.
What happens to deferred functions when a panic occurs?
AThey cause another panic
BThey are skipped
CThey run only if recover is called
DThey run normally
✗ Incorrect
Deferred functions run even during panic stack unwinding.
When is it appropriate to use panic in Go?
AFor serious, unrecoverable errors
BTo log messages
CTo start goroutines
DFor normal error handling
✗ Incorrect
panic is for serious errors that should stop the program.
What does stack unwinding mean in the context of panic?
AStarting new functions
BIgnoring errors
CRunning deferred functions while exiting functions
DCreating new goroutines
✗ Incorrect
Stack unwinding means running deferred functions while exiting each function.
Explain how panic and recover work together in Go to handle unexpected errors.
Think about how you can stop a crash and keep the program running.
You got /4 concepts.
Describe when it is best to use panic instead of returning an error in Go.
Consider the difference between a crash and a normal mistake.
You got /4 concepts.
Practice
(1/5)
1. What happens when a panic is called in a Go program?
easy
A. The program ignores the panic and continues running.
B. The program restarts automatically.
C. The program stops immediately and prints an error message.
D. The program logs the error but continues execution.
Solution
Step 1: Understand panic behavior
In Go, calling panic immediately stops normal execution and begins panicking.
Step 2: Effect on program flow
The program prints a stack trace and error message, then terminates unless recovered.
Final Answer:
The program stops immediately and prints an error message. -> Option C
Quick Check:
Panic stops program = B [OK]
Hint: Panic always stops the program immediately [OK]
Common Mistakes:
Thinking panic allows program to continue
Confusing panic with error return
Assuming panic restarts the program
2. Which of the following is the correct syntax to cause a panic with the message "error occurred"?
easy
A. panic("error occurred";)
B. panic("error occurred")
C. panic: "error occurred"
D. panic = "error occurred"
Solution
Step 1: Recall panic syntax
Panic is a built-in function called with parentheses and a string argument.
Step 2: Check each option
panic("error occurred") uses correct function call syntax. Others have invalid assignment, colon, or extra semicolon.
Final Answer:
panic("error occurred") -> Option B
Quick Check:
Panic is a function call = D [OK]
Hint: Panic is called like a function with parentheses [OK]
Common Mistakes:
Using assignment instead of function call
Adding extra semicolons inside panic call
Using colon instead of parentheses
3. What will be the output of this Go program?
package main
import "fmt"
func main() {
fmt.Println("Start")
panic("something went wrong")
fmt.Println("End")
}
medium
A. Start
End
B. something went wrong
panic stack trace
C. Start
D. Start
something went wrong
panic stack trace
Solution
Step 1: Trace program execution
The program prints "Start" first, then calls panic with a message.
Step 2: Effect of panic on following code
After panic, the program stops immediately, so "End" is not printed. Panic message and stack trace appear.
Final Answer:
Start
something went wrong
panic stack trace -> Option D
Quick Check:
Panic stops after printing Start = C [OK]
Hint: Panic prints message and stops; code after panic not run [OK]
Common Mistakes:
Expecting code after panic to run
Ignoring panic message output
Confusing panic output with normal print
4. Identify the error in this Go code snippet:
func divide(a, b int) int {
if b == 0 {
panic("division by zero")
}
return a / b
}
func main() {
result := divide(10, 0)
fmt.Println(result)
}
medium
A. Missing import of "fmt" package
B. Cannot panic inside a function
C. Division by zero is allowed, no panic needed
D. Function divide should return error instead of int
Solution
Step 1: Check code for syntax errors
The code uses fmt.Println but does not import "fmt" package.
Step 2: Validate panic usage
Panic is allowed inside functions; division by zero should panic to avoid runtime error.
Final Answer:
Missing import of "fmt" package -> Option A
Quick Check:
Missing import causes compile error = A [OK]
Hint: Check imports when using fmt functions [OK]
Common Mistakes:
Thinking panic is not allowed in functions
Ignoring missing imports
Assuming division by zero returns zero
5. You want to write a Go function that safely divides two integers and returns the result. If the divisor is zero, it should panic with "division by zero". Which of these implementations correctly uses panic and recover to handle the error and return zero instead of crashing the program?
func safeDivide(a, b int) int {
// Your code here
}
func main() {
fmt.Println(safeDivide(10, 0))
}
hard
A. func safeDivide(a, b int) int {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered from panic")
}
}()
if b == 0 {
panic("division by zero")
}
return a / b
}
B. func safeDivide(a, b int) int {
if b == 0 {
panic("division by zero")
}
return a / b
}
C. func safeDivide(a, b int) int {
defer func() {
if r := recover(); r != nil {
return 0
}
}()
if b == 0 {
panic("division by zero")
}
return a / b
}
D. func safeDivide(a, b int) int {
if b == 0 {
return 0
}
return a / b
}
Solution
Step 1: Understand panic and recover usage
Recover must be called inside a deferred function to catch panic and prevent program crash.
Step 2: Analyze each option
func safeDivide(a, b int) int {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered from panic")
}
}()
if b == 0 {
panic("division by zero")
}
return a / b
} uses defer with recover to catch panic and prints a message, then continues. It returns the division result or panics if divisor is zero. func safeDivide(a, b int) int {
defer func() {
if r := recover(); r != nil {
return 0
}
}()
if b == 0 {
panic("division by zero")
}
return a / b
} tries to return inside defer, which does not affect the outer function's return. func safeDivide(a, b int) int {
if b == 0 {
panic("division by zero")
}
return a / b
} panics without recovery. func safeDivide(a, b int) int {
if b == 0 {
return 0
}
return a / b
} returns 0 without panic.
Final Answer:
Option A correctly uses panic and recover to handle error and avoid crash. -> Option A
Quick Check:
Defer recover catches panic = A [OK]
Hint: Use defer with recover to catch panic safely [OK]
Common Mistakes:
Returning inside defer does not change function return
Not using recover causes program crash
Ignoring panic and returning zero silently
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.