In this chapter, we dive deep into advanced error handling techniques in Go, including error wrapping with context, using third-party packages like pkg/errors, and best practices in logging strategies and tools.
Chapter 3: Advanced Error Handling in Go – MCQs
1. Wrapping Errors with Context (10 Questions)
What is the purpose of wrapping errors in Go? a) To add more context to the error b) To change the type of the error c) To propagate the error to another function d) To log the error automatically
How can you wrap an existing error in Go? a) Using fmt.Errorf with %w verb b) Using errors.New() c) Using error.Wrap() d) Using fmt.Print()
Which of the following is true about error wrapping in Go? a) Wrapping an error adds extra details without changing the original error b) Wrapping an error changes the original error c) Wrapping errors is not allowed in Go d) Wrapped errors are ignored by defer statements
How do you check the cause of a wrapped error in Go? a) Using errors.Cause() b) Using fmt.Print() c) Using error.Unwrap() d) Both a and c
What does the Unwrap() function do in Go’s error handling? a) Extracts the original error from a wrapped error b) Logs the error automatically c) Changes the error type d) Catches panic in a function
In which Go version was error wrapping with %w introduced? a) Go 1.8 b) Go 1.10 c) Go 1.13 d) Go 1.15
What happens if an error is wrapped multiple times? a) The original error is lost b) Only the most recent wrap is available c) The original error can still be accessed using errors.Unwrap() d) All wrapped errors are concatenated into a single message
How can you use fmt.Errorf to wrap an error with context? a) fmt.Errorf("context: %w", err) b) fmt.Errorf("context: %s", err) c) fmt.Errorf("context %w: %s", err) d) fmt.Errorf("context: %v", err)
Why is it important to provide context when wrapping errors? a) It helps to log the error b) It allows tracing the error’s origin c) It prevents panic d) It reduces the error message size
Which function is used to wrap an error with additional context and cause in Go? a) errors.Wrap() b) errors.New() c) fmt.Errorf() d) error.Wrap()
2. Third-party Error Packages (10 Questions)
What is the primary use of third-party error packages in Go? a) To standardize error messages b) To log errors automatically c) To add additional functionality to error handling d) To replace the standard error type
Which package provides the Wrap function to wrap errors with context? a) errors b) fmt c) pkg/errors d) log
What does the pkg/errors package offer for error handling? a) Error wrapping b) Stack traces c) Error formatting d) All of the above
Which of the following is a benefit of using pkg/errors over the standard Go error handling? a) Automatic logging of errors b) Support for error wrapping and stack traces c) Error message shortening d) Better error propagation
How do you add a stack trace to an error using pkg/errors? a) errors.Wrap(err, "message") b) errors.AddStackTrace(err) c) errors.NewStackTrace(err) d) errors.WithStack(err)
What is the purpose of the errors.Wrapf() function in the pkg/errors package? a) To format an error message without changing the original error b) To create a new error with no context c) To log the error automatically d) To create an error with a custom error code
Which of the following does the pkg/errors package help you avoid? a) Writing verbose error messages b) Adding context and stack traces to every error c) Handling all errors in one place d) Ignoring errors completely
How do you check if an error is of a specific type in the pkg/errors package? a) Using errors.Is() b) Using errors.As() c) Using errors.Unwrap() d) Using errors.Compare()
Which Go tool is commonly used alongside third-party error packages for error handling? a) go test b) log c) fmt d) pkg/errors
What is one disadvantage of using third-party error packages like pkg/errors? a) It increases code verbosity b) It does not support stack traces c) It is not compatible with all Go versions d) It reduces error handling capabilities
3. Logging Strategies and Tools (10 Questions)
What is the primary goal of logging in Go? a) To log successful function calls b) To track program flow c) To capture and store error details for debugging d) To replace error handling
Which Go package is commonly used for logging in Go applications? a) log b) fmt c) errors d) logger
What is the difference between log.Fatal() and log.Panic() in Go? a) log.Fatal() writes the error to the log and terminates the program, log.Panic() triggers a panic b) log.Panic() writes the error and terminates the program c) log.Fatal() writes the error without terminating the program d) There is no difference
How can you configure logging for different log levels (e.g., info, error) in Go? a) By using the log package b) By using third-party logging packages like logrus c) By setting custom log levels manually d) By overriding the fmt package
Which of the following is an example of structured logging in Go? a) log.Println("Error occurred") b) log.Printf("Error at %s: %s", time.Now(), err) c) log.Fatal("Critical error!") d) log.Debug("Debugging...")
What is a key benefit of structured logging? a) Easier to read and search logs b) Logs are stored in plain text c) Reduced error handling complexity d) Logs are ignored during program execution
Which logging tool in Go supports JSON logging out of the box? a) log b) logrus c) fmt d) zap
How can you write custom log formats in Go? a) By using the log package’s SetFlags() function b) By using third-party packages like logrus or zap c) By writing to a log file manually d) By using the fmt package
What is the advantage of using logrus over the built-in log package? a) It supports log rotation b) It supports structured logging and multiple log levels c) It is more lightweight d) It automatically handles error messages
What is the purpose of using defer in logging? a) To delay logging messages until after the function completes b) To stop logging messages c) To change the log level dynamically d) To handle panic situations in logging
Answers
QNo
Answer (Option with the text)
1
a) To add more context to the error
2
a) Using fmt.Errorf with %w verb
3
a) Wrapping an error adds extra details without changing the original error
4
d) Both a and c
5
a) Extracts the original error from a wrapped error
6
c) Go 1.13
7
c) The original error can still be accessed using errors.Unwrap()
8
a) fmt.Errorf("context: %w", err)
9
b) It allows tracing the error’s origin
10
c) fmt.Errorf()
11
c) To add additional functionality to error handling
12
c) pkg/errors
13
d) All of the above
14
b) Support for error wrapping and stack traces
15
d) errors.WithStack(err)
16
a) errors.Wrap(err, "message")
17
b) Adding context and stack traces to every error
18
d) Using errors.Compare()
19
a) go test
20
c) It is not compatible with all Go versions
21
c) To capture and store error details for debugging
22
a) log
23
a) log.Fatal() writes the error to the log and terminates the program, log.Panic() triggers a panic
24
b) By using third-party logging packages like logrus
25
b) log.Printf("Error at %s: %s", time.Now(), err)
26
a) Easier to read and search logs
27
b) logrus
28
b) By using third-party packages like logrus or zap
29
b) It supports structured logging and multiple log levels
30
a) To delay logging messages until after the function completes