
In Go, error handling is managed using defer, panic, and recover. These mechanisms help in handling unexpected errors and ensuring proper resource cleanup.
defer Statementđš defer is used to delay the execution of a function until the surrounding function returns. It is mainly used for cleanup operations like closing files, releasing locks, etc.
deferdefer functionName()
defer statements are executed in LIFO (Last In, First Out) order.deferpackage main
import "fmt"
func main() {
fmt.Println("Start")
defer fmt.Println("Deferred: This executes last")
fmt.Println("Middle")
}
đš Output:
Start
Middle
Deferred: This executes last
đ Explanation:
"Deferred: This executes last" runs at the end because it is deferred.defer Example (LIFO Order)package main
import "fmt"
func main() {
defer fmt.Println("First")
defer fmt.Println("Second")
defer fmt.Println("Third")
fmt.Println("Main function executed")
}
đš Output:
Main function executed
Third
Second
First
đ Explanation:
defer statement is executed first.panic in Gođš panic is used to stop program execution immediately.
đš It is usually used when the program cannot recover from an error.
đš When panic occurs, it executes deferred statements before stopping the program.
panicpanic("Error message")
panicpackage main
import "fmt"
func main() {
fmt.Println("Start")
panic("Something went wrong!") // Program stops here
fmt.Println("End") // This line will not execute
}
đš Output:
Start
panic: Something went wrong!
đ Explanation:
"Start" prints.panic("Something went wrong!") stops execution."End" does not execute.panic with defer (Deferred function still runs)package main
import "fmt"
func main() {
defer fmt.Println("This will execute before panic stops the program")
fmt.Println("Before panic")
panic("Something went wrong!")
}
đš Output:
Before panic
This will execute before panic stops the program
panic: Something went wrong!
đ Explanation:
panic stops execution.recover in Gođš recover is used to catch a panic and prevent the program from crashing.
đš It is used inside a defer function to handle errors gracefully.
recoverrecover()
recover() returns nil if there is no panic.recover Handling Panicpackage main
import "fmt"
func main() {
defer handlePanic()
fmt.Println("Start")
panic("Something went wrong!")
fmt.Println("End") // This line will NOT execute
}
func handlePanic() {
if r := recover(); r != nil {
fmt.Println("Recovered from panic:", r)
}
}
đš Output:
Start
Recovered from panic: Something went wrong!
đ Explanation:
panic("Something went wrong!") occurs.handlePanic() runs because of defer.recover() catches the panic and prevents a crash.panic, defer, and recover Togetherpackage main
import "fmt"
func main() {
fmt.Println("Main started")
safeFunction()
fmt.Println("Main function continues")
}
func safeFunction() {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered from:", r)
}
}()
fmt.Println("Inside safeFunction")
panic("Critical error!") // Triggers panic
fmt.Println("This line will not execute")
}
đš Output:
Main started
Inside safeFunction
Recovered from: Critical error!
Main function continues
đ Explanation:
panic("Critical error!") is triggered.recover() inside defer catches the panic.| Feature | Description |
|---|---|
defer | Delays execution of a function until the surrounding function completes. Useful for cleanup tasks. |
panic | Stops program execution immediately. Used for critical errors. |
recover | Captures panic and prevents program crash. Used inside defer. |
defer āĻā§?đš defer āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰāĻž āĻšāϝāĻŧ āĻā§āύ⧠āĻĢāĻžāĻāĻļāύā§āϰ āĻāĻā§āϏāĻŋāĻāĻŋāĻāĻļāύ āĻŦāĻŋāϞāĻŽā§āĻŦāĻŋāϤ āĻāϰāϤā§āĨ¤
đš āϏāĻžāϧāĻžāϰāĻŖāϤ āĻĢāĻžāĻāϞ āĻā§āϞā§āĻ, āϰāĻŋāϏā§āϰā§āϏ āϰāĻŋāϞāĻŋāĻ āĻāϰāĻžāϰ āĻāύā§āϝ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰāĻž āĻšāϝāĻŧāĨ¤
defer āĻāϰ āĻāĻĻāĻžāĻšāϰāĻŖpackage main
import "fmt"
func main() {
fmt.Println("Start")
defer fmt.Println("āĻāĻāĻŋ āϏāĻŦāĻļā§āώ⧠āĻāϞāĻŦā§")
fmt.Println("Middle")
}
đš āĻāĻāĻāĻĒā§āĻ:
Start
Middle
āĻāĻāĻŋ āϏāĻŦāĻļā§āώ⧠āĻāϞāĻŦā§
panic āĻā§?đš panic āĻšāϞ⧠āĻā§āϞ āĻšāϞ⧠āĻĒā§āϰā§āĻā§āϰāĻžāĻŽ āĻĨāĻžāĻŽāĻžāύā§āϰ āĻāύā§āϝāĨ¤
đš āĻāĻāĻŋ āĻāĻā§āϏāĻŋāĻāĻŋāĻāĻļāύ āϤāĻžā§āĻā§āώāĻŖāĻŋāĻāĻāĻžāĻŦā§ āĻŦāύā§āϧ āĻāϰā§āĨ¤
panic āĻāϰ āĻāĻĻāĻžāĻšāϰāĻŖpackage main
import "fmt"
func main() {
fmt.Println("Start")
panic("āϏāĻŽāϏā§āϝāĻž āĻĻā§āĻāĻž āĻĻāĻŋāϝāĻŧā§āĻā§!") // āĻĒā§āϰā§āĻā§āϰāĻžāĻŽ āĻāĻāĻžāύ⧠āĻĨā§āĻŽā§ āϝāĻžāĻŦā§
fmt.Println("End") // āĻāĻāĻŋ āĻāĻā§āϏāĻŋāĻāĻŋāĻāĻ āĻšāĻŦā§ āύāĻž
}
đš āĻāĻāĻāĻĒā§āĻ:
Start
panic: āϏāĻŽāϏā§āϝāĻž āĻĻā§āĻāĻž āĻĻāĻŋāϝāĻŧā§āĻā§!
recover āĻā§?đš recover āĻšāϞ⧠panic āĻā§āϝāĻžāĻĒāĻāĻžāϰ āĻāϰāĻžāϰ āĻāĻĒāĻžāϝāĻŧāĨ¤
đš āĻāĻāĻŋ defer āĻāϰ āĻāĻŋāϤāϰ⧠āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰāĻž āĻšāϝāĻŧ panic āĻĨā§āĻā§ āϰāĻā§āώāĻž āĻĒā§āϤā§āĨ¤
recover āĻāϰ āĻāĻĻāĻžāĻšāϰāĻŖpackage main
import "fmt"
func main() {
defer handlePanic()
fmt.Println("Start")
panic("āĻāĻŋāĻā§ āĻāĻāĻāĻž āϏāĻŽāϏā§āϝāĻž āĻšāϝāĻŧā§āĻā§!")
}
func handlePanic() {
if r := recover(); r != nil {
fmt.Println("āĻĒā§āϝāĻžāύāĻŋāĻ āĻĨā§āĻā§ āĻāĻĻā§āϧāĻžāϰ:", r)
}
}
đš āĻāĻāĻāĻĒā§āĻ:
Start
āĻĒā§āϝāĻžāύāĻŋāĻ āĻĨā§āĻā§ āĻāĻĻā§āϧāĻžāϰ: āĻāĻŋāĻā§ āĻāĻāĻāĻž āϏāĻŽāϏā§āϝāĻž āĻšāϝāĻŧā§āĻā§!
â
defer â āĻĻā§āϰāĻŋāϤ⧠āĻāĻžāϞāĻžāϝāĻŧ
â
panic â āϤāĻžā§āĻā§āώāĻŖāĻŋāĻāĻāĻžāĻŦā§ āĻĨāĻžāĻŽāĻžāϝāĻŧ
â
recover â panic āĻ āĻŋāĻ āĻāϰā§
đ Go-āϤ⧠defer, panic, āĻ recover āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰ⧠āĻāĻĢāĻŋāĻļāĻŋāϝāĻŧā§āύā§āĻ āĻāϰāϰ āĻšā§āϝāĻžāύā§āĻĄāϞāĻŋāĻ āĻāϰāĻž āϝāĻžāϝāĻŧāĨ¤