Skip to content

When to Throw Exceptions

Question

When should you throw an exception? Is there a downside to exception-driven development?

Short interview answer

Throw for exceptional failure states that the caller cannot handle as normal flow. Do not use exceptions for routine expected outcomes; use validation, Try* APIs, or explicit results where appropriate.

Detailed answer

An invalid argument or impossible object state may justify a specific exception. A missing optional lookup result usually does not. Exceptions carry stack and allocation costs and make normal control flow harder to read; they also obscure which outcomes callers are expected to handle. A pre-check can still race with the operation, so exceptions may remain necessary even when a condition is checked first.

Sources