Glossary / Exceptions

Exceptions

Exceptions represent failures or exceptional conditions that interrupt normal control flow. C# uses try, catch, finally, and throw; robust exception handling preserves diagnostic context, releases resources, and distinguishes recoverable conditions from programming defects.

Propagation and stack behavior

When an exception is thrown, the runtime searches up the call stack for a compatible catch handler. finally executes while unwinding, even when control leaves through return or exception, except catastrophic process termination. throw; rethrows the current exception preserving its stack; throw ex; resets the visible throw origin and should generally be avoided. Exception.InnerException preserves causal failures when wrapping.

Catch the narrowest exception that can be meaningfully handled. Empty catch blocks hide failures. Do not use exceptions for ordinary expected control flow when a result type or Try... API is clearer. Exceptions can cross async boundaries through faulted Tasks and are rethrown when awaited. Some fatal conditions such as stack overflow cannot be reliably recovered from.

Resource safety and observability

Use using/finally for cleanup. Add context when translating exceptions, preserving the original as inner exception. Log once at an ownership boundary to avoid duplicate noisy logs; include operation identifiers but avoid secrets. Define retry policy by exception category, idempotency, and deadlines.

Interview questions

  1. throw; vs throw ex;? Bare throw preserves the original stack trace; throwing the variable resets the throw site.
  2. When use finally? Cleanup that must occur regardless of success or failure.
  3. Should catch Exception broadly? Only at boundaries where you can log/translate/terminate thoughtfully; otherwise catch specific failures.
  4. How preserve causal context? Wrap with the original exception as InnerException.
  5. Are exceptions appropriate for expected missing values? Usually a result/nullable/Try pattern is clearer.

References