Challenge - 5 Problems
Error Handling Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of error handling with multiple returns
What is the output of this Go program when the function returns an error?
Go
package main import ( "errors" "fmt" ) func checkValue(val int) (string, error) { if val < 0 { return "", errors.New("negative value") } return "Value is positive", nil } func main() { msg, err := checkValue(-5) if err != nil { fmt.Println("Error:", err) } else { fmt.Println(msg) } }
Attempts:
2 left
💡 Hint
Check what the function returns when the input is negative.
✗ Incorrect
The function returns an error when the input is less than zero. The main function prints the error message.
❓ Predict Output
intermediate2:00remaining
Result of deferred function with error handling
What will be printed by this Go program?
Go
package main import "fmt" func main() { var err error defer func() { if err != nil { fmt.Println("Deferred error:", err) } else { fmt.Println("No error") } }() err = fmt.Errorf("something went wrong") fmt.Println("Main function running") }
Attempts:
2 left
💡 Hint
Remember when deferred functions run and the value of err at that time.
✗ Incorrect
The deferred function runs after main prints "Main function running". By then, err is set to the error, so it prints the error message.
🔧 Debug
advanced2:00remaining
Identify the runtime error in error handling
What runtime error will this Go program produce?
Go
package main import ( "errors" "fmt" ) func riskyOperation() error { return errors.New("operation failed") } func main() { var err error err = riskyOperation() if err != nil { panic(err.Error()) } fmt.Println("Operation succeeded") }
Attempts:
2 left
💡 Hint
Check what happens when panic is called with an error message.
✗ Incorrect
The program calls panic with the error message, causing a runtime panic with that message.
🧠 Conceptual
advanced2:00remaining
Understanding error wrapping and unwrapping
Given the following code, what will be the output?
Go
package main import ( "errors" "fmt" ) func main() { baseErr := errors.New("base error") wrappedErr := fmt.Errorf("wrapped: %w", baseErr) if errors.Is(wrappedErr, baseErr) { fmt.Println("Error matches base error") } else { fmt.Println("Error does not match") } }
Attempts:
2 left
💡 Hint
Look at how %w is used in fmt.Errorf and how errors.Is works.
✗ Incorrect
The %w verb wraps the base error inside wrappedErr. errors.Is detects this and returns true.
❓ Predict Output
expert3:00remaining
Output of concurrent error handling with channels
What will this Go program print?
Go
package main import ( "errors" "fmt" "sync" ) func worker(id int, ch chan error, wg *sync.WaitGroup) { defer wg.Done() if id%2 == 0 { ch <- nil } else { ch <- errors.New(fmt.Sprintf("error from worker %d", id)) } } func main() { ch := make(chan error, 5) var wg sync.WaitGroup for i := 1; i <= 5; i++ { wg.Add(1) go worker(i, ch, &wg) } wg.Wait() close(ch) countErrors := 0 for err := range ch { if err != nil { fmt.Println(err.Error()) countErrors++ } } fmt.Println("Total errors:", countErrors) }
Attempts:
2 left
💡 Hint
Check which workers send errors and which send nil, and how many errors are counted.
✗ Incorrect
Workers with odd IDs send errors, even IDs send nil. The program counts and prints only non-nil errors.