0
0
Goprogramming~20 mins

Why error handling is required in Go - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Go Error Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why do we need error handling in Go?

Imagine you are writing a program that reads a file. Sometimes the file might not exist or be unreadable. Why is it important to handle such errors in Go?

ATo ignore errors and continue running the program blindly
BTo detect problems and respond properly, like showing a message or trying again
CTo make sure the program crashes immediately without any message
DTo slow down the program intentionally
Attempts:
2 left
💡 Hint

Think about what happens if the program tries to read a missing file without checking for errors.

Predict Output
intermediate
2:00remaining
What is the output of this Go code with error handling?

Look at this Go code that tries to open a file. What will it print if the file does not exist?

Go
package main

import (
	"fmt"
	"os"
)

func main() {
	file, err := os.Open("nofile.txt")
	if err != nil {
		fmt.Println("Error opening file:", err)
		return
	}
	defer file.Close()
	fmt.Println("File opened successfully")
}
AError opening file: open nofile.txt: no such file or directory
Bpanic: runtime error
CNo output
DFile opened successfully
Attempts:
2 left
💡 Hint

What does the program do when err is not nil?

🔧 Debug
advanced
2:00remaining
Identify the error handling mistake in this Go code

This Go code tries to read a file but has a mistake in error handling. What is the problem?

Go
package main

import (
	"fmt"
	"os"
)

func main() {
	file, err := os.Open("data.txt")
	if err == nil {
		fmt.Println("Error opening file")
		return
	}
	defer file.Close()
	fmt.Println("File opened successfully")
}
AThere is no mistake; the code is correct
BThe file.Close() should not be deferred
CThe error check is reversed; it should be 'if err != nil' instead of 'if err == nil'
DThe program should panic instead of returning
Attempts:
2 left
💡 Hint

Remember, err is nil when there is no error.

📝 Syntax
advanced
2:00remaining
Which option causes a compile-time error in Go error handling?

Which of these Go code snippets will cause a compile-time error?

A
file, err := os.Open("file.txt")
if err != nil {
	fmt.Println(err)
}
B
file, err := os.Open("file.txt")
if err != nil {
	fmt.Println(err)
} else {
	defer file.Close()
}
C
file, err := os.Open("file.txt")
if err != nil {
	fmt.Println(err)
} else {
	file.Close()
}
D
file, err := os.Open("file.txt")
if err != nil {
	fmt.Println(err)
}
else
	defer file.Close()
Attempts:
2 left
💡 Hint

Check the syntax rules for if-else statements and defer usage.

🚀 Application
expert
3:00remaining
What is the value of 'result' after running this Go function with error handling?

Consider this Go function that divides two numbers and returns an error if dividing by zero. What is the value of 'result' after calling divide(10, 0)?

Go
package main

import (
	"errors"
	"fmt"
)

func divide(a, b int) (int, error) {
	if b == 0 {
		return 0, errors.New("cannot divide by zero")
	}
	return a / b, nil
}

func main() {
	result, err := divide(10, 0)
	if err != nil {
		fmt.Println("Error:", err)
		result = -1
	}
	fmt.Println("Result:", result)
}
AResult: -1
BResult: 0
CResult: 10
DProgram crashes with panic
Attempts:
2 left
💡 Hint

What happens when divide returns an error? How is 'result' changed?