tezvyn:

do-try-catch versus Optional return types

Source: interviewintermediate

WHAT IT TESTS: error modeling choices. OUTLINE: throwing functions with do/try/catch convey why something failed via typed Error values; Optionals only say success or absence. Use throws when the caller needs the reason.

WHAT IT TESTS: choosing the right failure representation. ANSWER OUTLINE: a throwing function marked throws propagates an Error you handle with do/try/catch, carrying rich context about why it failed; an Optional return signals only presence or absence with no reason attached. Prefer throws when the caller must know and react to the cause, and prefer Optional when absence is an ordinary, expected non-error outcome like a missing dictionary key. RED FLAG: throwing for benign absence or swallowing real errors as nil.

Read the original → interview

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

do-try-catch versus Optional return types · Tezvyn