Python Explicit Error Handling, Wrapping & Inspection
Go treats errors as regular values rather than hidden exceptions. Master returning (result, error), wrapping errors with fmt.Errorf("%w"), and inspecting error chains with errors.Is and errors.As.
"Instead of an unexpected building fire alarm (exceptions), every room inspection produces a signed report card. If a defect is found, the inspector attaches a note and hands it up."
Deep Dive: How It Works
Errors as Values: error is a standard built-in interface with Error() string.
Explicit Handling: Errors are returned as the final return value and handled immediately with if err != nil.
Error Wrapping with %w: fmt.Errorf("ctx: %w", err) wraps errors so errors.Is and errors.As can unwrap them.
Syntax Blueprint
var ErrNotFound = errors.New("not found")
wrapped := fmt.Errorf("query failed: %w", ErrNotFound)
if errors.Is(wrapped, ErrNotFound) { ... }Create sentinel errors, wrap with %w context, and inspect using errors.Is.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Using %v instead of %w when formatting errors with fmt.Errorf.Why it happens: %v converts error to string, discarding the underlying error type chain needed for errors.Is.
How to fix: Always use %w when wrapping underlying errors.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Safe Division with Custom Error
Define a sentinel error var ErrDivideByZero = errors.New("cannot divide by zero").
Write a function divide(a, b float64) (float64, error) that returns ErrDivideByZero if b == 0.
In main(), call divide(10, 0), check if errors.Is(err, ErrDivideByZero), and print "Caught: divide by zero".
Finished reading and practicing?
Mark this lesson as completed to update your course progress.