Senior
// Question 2. What will be displayed on the screen package main import "fmt" func bbb() { defer func(){ if r := recover(); r != nil{ fmt.Println("Panic handled 1 ", r) } }() panic("aaaa") } func main() { defer func(){ if r := recover(); r != nil{ fmt.Println("Panic handled 2 ", r) } }() bbb() }
sobes.tech AI
Answer from AI
In this code, the function bbb calls panic("aaaa"), but before that, an defer with an anonymous function is defined, which calls recover() and handles the panic, printing the message:
Panic handled 1 aaaa
Since the panic is handled inside bbb, it does not propagate further in main, and the second defer with recover in main does not trigger.
The final output on the screen will be:
Panic handled 1 aaaa
If recover in bbb was not present, then the panic would reach main and be handled there with the output "Panic handled 2 aaaa".