Go Error Handling and Panic/Recover Essentials Quiz Quiz

Discover key principles of error handling and the panic/recover mechanism in Go. This quiz helps reinforce understanding of error values, panic usage, recover patterns, and idiomatic approaches in Go programming.

  1. Go Built-in Error Interface

    Which interface in Go is used to represent an error value returned from functions?

    1. error
    2. Err
    3. Exception
    4. fail

    Explanation: The 'error' interface is built into Go for error handling and is commonly used as a return type for functions, allowing errors to be checked. 'Exception' and 'fail' do not exist in the Go standard library, and 'Err' is not an interface but may be used as a variable name. Only 'error' is correct for this purpose.

  2. Idiomatic Error Handling

    In Go, what is the standard way to handle a function that returns both a result and an error?

    1. Ignore the error value
    2. Check if error is nil before using the result
    3. Throw the error immediately
    4. Use a try-catch block

    Explanation: Go code typically checks if the error returned from a function is nil before using the result, ensuring errors are caught early. Go does not support try-catch blocks like some other languages. Ignoring errors or always throwing them is not recommended and goes against idiomatic Go practices.

  3. Creating Custom Error Types

    Which method enables you to define a custom error type in Go?

    1. Call MakeErrorFunction
    2. Implement the Error() string method
    3. Define an Exception struct
    4. Use error.NewType

    Explanation: To create a custom error type in Go, you implement the Error() string method for your type, satisfying the error interface. 'Exception struct' and 'MakeErrorFunction' are not recognized patterns in Go, and there is no 'error.NewType' method available.

  4. Using the panic Function

    What does the panic function do in a Go program?

    1. Restarts the Go application
    2. Returns an error value to the caller
    3. Immediately stops normal execution and starts unwinding the stack
    4. Silently logs a warning

    Explanation: Calling panic causes the program to halt normal execution and begins to unwind the stack, triggering deferred functions. Panic does not just return an error, log a warning, or restart the application—those options describe unrelated or incorrect behaviors.

  5. Recovering from Panics

    Where should the recover function typically be called in order to handle a panic?

    1. Inside a deferred function
    2. Within a for loop
    3. Global scope at the top level
    4. In the main function directly

    Explanation: Recover must be called inside a deferred function to intercept a panic and regain control. If used anywhere else, recover will not catch the panic. Calling it in main, in a for loop, or at the global level is incorrect and will not work as intended.

  6. Zero Values and Errors

    What is the zero value of the error type in Go?

    1. zero
    2. false
    3. empty string
    4. nil

    Explanation: Before an error is assigned, its zero value is nil, which Go uses to indicate no error is present. An empty string, false, or zero are not valid zero values for the error type, as error is an interface, not a primitive data type.

  7. Panic vs. Error

    Which is the most suitable scenario to use panic instead of returning an error?

    1. Input validation failures
    2. Routine parsing errors
    3. Missing configuration files
    4. Encountering unrecoverable program bugs

    Explanation: Panic is intended for unrecoverable errors that indicate a programming bug or a state that cannot be gracefully handled. Input validation, missing files, and parsing errors should return error values, as they are expected and can be managed more gracefully.

  8. Multiple Return Values

    Which standard Go pattern enables functions to return both useful results and error information?

    1. Returning (value, error)
    2. Returning error codes only
    3. Using global error variables
    4. Throwing exceptions

    Explanation: Functions often return a tuple of (value, error), allowing callers to check for errors before using the result. Global error variables are prone to conflicts, returning only error codes lacks context, and Go does not support exception throwing in the standard sense.

  9. Error Formatting

    Which function would you use to create a formatted error value that includes context information?

    1. fmt.Errorf
    2. panicf
    3. log.Fatal
    4. error.NewFormatted

    Explanation: fmt.Errorf is used to create formatted error messages, providing valuable context with variables. log.Fatal terminates the program and is not for error creation, panicf is not a standard function, and error.NewFormatted does not exist in Go's libraries.

  10. Behavior of Recover Outside defer

    What will happen if recover is called outside of a deferred function during a panic in Go?

    1. It returns nil and does not stop the panic
    2. It logs the panic to stderr
    3. It restarts the main function
    4. It cancels the panic successfully

    Explanation: Recover only works inside a deferred function during panic unwinding. If called outside of this context, it returns nil and has no effect. It will not cancel the panic, restart main, or automatically log the panic anywhere.