typeover
curriculum

Curriculum Errors try and catch exercise 2 · mcq

try and catch

When you'd rather substitute a DEFAULT VALUE for an error (like TypeScript's await fetchN().catch(() => 0)), Zig has <expr> catch <default> — exactly mirroring the optional's orelse. Pick the right shape.

TypeScript reference
Pick the idiomatic Go translation

About this theme

There are three ways to consume an error union. try expr is the propagator: on error, return the error to the caller; on success, unwrap the value. expr catch <default> substitutes a fallback value (analogous to optionals orelse). expr catch |err| { ... } captures the error and runs a handler block — pair with a switch on err to handle each case. Once these three are reflex, error handling reads cleanly without exception-handler ceremony.