Error Handling Questions with Answers

11 practice questions on Error Handling (Go) with answers and step-by-step solutions. Try 10 samples below, then practise all 11 free in the AptiNest app.

Sample questions

Question 1Easy

What does the blank identifier in v, _ := f() do with the error?

  1. Converts it to a zero value of v
  2. Discards it, which hides failures if f can fail
  3. Logs it automatically to standard error, so the failure is still visible when debugging
  4. Panics if the error is non-nil

Answer: B. Discards it, which hides failures if f can fail

Explanation

Ignoring errors is legal but risky; linters flag unchecked errors.

Step-by-step solution

  1. The blank identifier _ ignores the second return value of f().
  2. Ignoring the error means any failure in f() is not checked.
  3. This hides failures, making it risky but legal in Go.
  4. So the error is discarded, which hides failures if f can fail.
  5. Final answer: Discards it, which hides failures if f can fail.

Why the other options are wrong

  • A. The blank identifier does not convert the error; it just ignores the second value.
  • C. Nothing is logged; the blank identifier writes nothing to stderr.
  • D. No panic happens; the error is simply dropped, and the program keeps running.

Tip: Use _ to ignore errors but be cautious as it hides potential issues.

Question 2Easy

How does Go typically report errors?

  1. As an extra error return value checked by the caller
  2. By setting a global errno variable that the caller must check after every function call
  3. With try/catch exceptions
  4. By printing and exiting

Answer: A. As an extra error return value checked by the caller

Explanation

val, err := f();
if err != nil { ... }

is the standard pattern.

Step-by-step solution

  1. Go has no exceptions.
  2. Functions return an error as the last value.
  3. Callers check if err != nil.

Why the other options are wrong

  • B. Go does not use errno.
  • C. Go has no try/catch; panic is for unrecoverable cases.
  • D. Printing and exiting is not the standard error mechanism.

Question 3Easy

A 'sentinel error' is:

  1. An error that stops the program immediately, like a panic, but can be caught by recover
  2. An error with a stack trace
  3. A predeclared error value, like io.EOF, that callers compare against
  4. A logging function

Answer: C. A predeclared error value, like io.EOF, that callers compare against

Explanation

Callers check errors.Is(err, io.EOF) to detect a specific condition.

Step-by-step solution

  1. A sentinel error is a fixed package-level error value.
  2. Example: io.EOF.
  3. Callers compare it with == or errors.Is.

Why the other options are wrong

  • A. It does not stop the program.
  • B. Stack traces are not part of the definition.
  • D. It is not a logging function.

Question 4Easy

What is the usual Go convention for a function that can fail?

  1. Return the error as the last return value
  2. Throw an exception with throw
  3. Return a negative number
  4. Set a global error flag

Answer: A. Return the error as the last return value

Explanation

Go has no exceptions for ordinary errors. A function returns (result, error) and callers check if err != nil.

Step-by-step solution

  1. Declare func f() (T, error).
  2. Return nil error on success.
  3. Caller tests err != nil.

Why the other options are wrong

  • B. Go has no throw statement.
  • C. Negative numbers do not carry details.
  • D. A global flag is not idiomatic.

Question 5Medium

What is the output?

package main

import (
	"errors"
	"fmt"
)

var ErrNotFound = errors.New("not found")

func main() {
	err := fmt.Errorf("load user: %w", ErrNotFound)
	fmt.Println(errors.Is(err, ErrNotFound), err)
}
  1. false load user: not found
  2. true not found
  3. Compile error
  4. true load user: not found

Answer: D. true load user: not found

Explanation

%w wraps the sentinel; errors.Is walks the chain and finds it.

Step-by-step solution

  1. errors.New creates ErrNotFound, and fmt.Errorf with %w wraps it inside err.
  2. The text of err is 'load user: not found'.
  3. errors.Is walks the wrapped chain, finds ErrNotFound, and returns true.
  4. Println prints 'true load user: not found', so the answer is option D.

Why the other options are wrong

  • A. errors.Is finds the wrapped error, so the result is true, not false.
  • B. The printed text includes the 'load user: ' prefix added by Errorf.
  • C. The code compiles; both imports are used.

Tip: %w keeps the original error inside the new one, so errors.Is can still match it.

Question 6Medium

Why is comparing error messages with strings (err.Error() == "not found") discouraged?

  1. Errors have no Error method
  2. It always returns true
  3. String comparison between error messages is not allowed in Go and fails to compile
  4. Messages can change and wrapping adds text, so the comparison breaks

Answer: D. Messages can change and wrapping adds text, so the comparison breaks

Explanation

Compare sentinel values or types with errors.Is and errors.As instead.

Step-by-step solution

  1. Error messages may change in future versions of code.
  2. Wrapping errors adds extra text to the message.
  3. This makes string comparisons unreliable and break easily.
  4. Comparing with errors.Is or errors.As handles these cases better.
  5. So comparing strings like err.Error() == "not found" is discouraged.

Why the other options are wrong

  • A. Errors do have an Error method; every error value provides it through the error interface.
  • B. The comparison does not always return true; it is true only when the text matches exactly.
  • C. The code compiles fine; the problem is fragility, not a compile error.

Tip: Use errors.Is or errors.As instead of string comparisons for errors.

Question 7Medium

errors.As is used to:

  1. Compare two error strings for equality, ignoring any context added by wrapping
  2. Find an error of a specific type in a wrapped chain and extract it
  3. Create a new error
  4. Convert an error to a string

Answer: B. Find an error of a specific type in a wrapped chain and extract it

Explanation

var pe *fs.PathError;
if errors.As(err, &pe) { ... }

gives access to the typed error's fields.

Step-by-step solution

  1. errors.As checks if an error matches a specific type in a wrapped chain.
  2. It allows extracting the specific error's fields from the chain.
  3. This is useful for handling errors of particular types accurately.
  4. The function returns true if the error matches the target type.
  5. This lets you access detailed information about the error directly.

Why the other options are wrong

  • A. Comparing error strings is not what errors.As does; it matches a type in the wrapped chain.
  • C. Creating an error is done with errors.New or fmt.Errorf, not errors.As.
  • D. Turning an error into text is done with err.Error, not errors.As.

Tip: Use errors.As to extract typed errors from chains for better error handling.

Question 8Medium

What is printed?

err := fmt.Errorf("read: %w", io.EOF)
fmt.Println(errors.Is(err, io.EOF))
  1. false
  2. read: EOF
  3. true
  4. Compile error

Answer: C. true

Explanation

%w wraps the error, and errors.Is follows the wrap chain, so it finds io.EOF.

Step-by-step solution

  1. Errorf with %w keeps the original error.
  2. errors.Is unwraps step by step.
  3. It matches io.EOF.

Why the other options are wrong

  • A. Wrapping preserves identity for Is.
  • B. Is returns a bool.
  • D. The code is valid.

Tip: Use %w, not %v, to wrap.

Question 9Medium

Good practice when returning an error up the stack is to:

  1. Print it and also return it at every level, so the full path appears in the logs
  2. Discard it and return nil
  3. Add context, e.g. fmt.Errorf("open config: %w", err)
  4. Panic immediately

Answer: C. Add context, e.g. fmt.Errorf("open config: %w", err)

Explanation

Context makes the final message useful; logging at every level causes duplicate noise.

Step-by-step solution

  1. When an error goes up the stack, it should gain context about what failed.
  2. fmt.Errorf with %w adds that context and keeps the original error for errors.Is.
  3. So adding context with %w is good practice, which is option C.

Why the other options are wrong

  • A. Logging the same error at every level repeats messages; handle an error once.
  • B. Discarding the error hides the failure.
  • D. Panicking on ordinary errors crashes the program; return them instead.

Tip: Wrap errors with context as they go up, and handle or log them once.

Question 10Hard

What is the output?

package main

import (
	"errors"
	"fmt"
)

func main() {
	e1 := errors.New("x")
	e2 := errors.New("x")
	fmt.Println(e1 == e2, e1.Error() == e2.Error())
}
  1. false true
  2. true false
  3. true true
  4. false false

Answer: A. false true

Explanation

errors.New returns distinct pointer values, so equal text does not make them equal errors.

Step-by-step solution

  1. errors.New returns a pointer to a new error value each time.
  2. e1 == e2 compares pointers, which differ: false.
  3. Error() strings are both 'x': true.

Why the other options are wrong

  • B. Pointers are different.
  • C. Same text does not mean same value.
  • D. The strings are equal.

Practise all 11 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Go topics · All topics