typeover
curriculum

Curriculum Errors Panics vs errors exercise 1 · mcq

Panics vs errors

TypeScript reaches for throw new Error(...) for both expected failures AND programmer bugs. Zig splits them: real failures use error unions; programmer bugs (impossible conditions) abort the program. Pick the right shape for a function whose CALLER might reasonably handle the failure case (empty input is a normal possibility).

TypeScript reference
Pick the idiomatic Go translation

About this theme

Errors are for conditions the caller should be expected to handle (bad input, out of memory, missing file). Panics are for conditions that indicate a programmer bug — invariants that must hold but didn't. @panic(msg) aborts the program with a message; unreachable is a no-message hint to the compiler that a code path can't be entered (in debug builds, hitting one panics with reached unreachable; in release builds it's UB). The discipline: errors for expected failures, panics for impossible-by-design conditions.