Curriculum Errors try and catch exercise 3 · mcq
try and catch
When you need to RUN code on the error path (log it, print
a message, take an alternate action) rather than just
substitute a default, use catch |err| { ... } — the
payload-capture form. Same pipe syntax as if (x) |y|
from optionals. Pick the right shape for a handler that
prints a message and returns from the caller.
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.