TRT
Sign in
easyCoreconcept

Why do you catch exceptions by reference and order handlers derived first?

Cover throw, unwinding with RAII, reference binding without slicing, and handler order from specific to general.

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team2 min read
Learn it firstThrowing and Catching: try, catch and throwPro — members onlySignal failures with exceptions, catch them by const reference in the right order, and keep throwing code safe with RAII.

A strong answer

A strong candidate frames Why do you catch exceptions by reference and order handlers derived first? around the exact memory layout, compiler semantics, and hardware execution model covered in Throwing and Catching: try, catch and throw. Cover throw, unwinding with RAII, reference binding without slicing, and handler order from specific to general.

throw creates an exception object and starts stack unwinding, shown in the trace: each frame's automatic objects are destroyed in reverse order until a matching catch is found. catch matches by type, first match wins, so derived types come before bases. Catching by const& avoids copies and slicing.

Derive custom errors from std::exception (or std::runtime_error when a message suffices) and override what() as const noexcept. The standard hierarchy already covers logic errors, invalid arguments, out-of-range access and runtime failures; reach for those before inventing a type.

In a production interview, always connect the specification rule to its concrete runtime consequence: state what lives on the stack versus the heap or static storage segment, how the compiler or operating system validates alignments, lifetimes, and error codes, and what happens under adversarial or boundary inputs (such as zero-length buffers, integer overflow, concurrent mutation, or partial I/O). Backing your explanation with a small trace of register/memory state transitions immediately separates senior systems engineers from candidates who only memorized syntax.

What the interviewer is listening for

  • Mastery of core invariant: Throw and catch standard and custom exceptions correctly.
  • Mastery of core invariant: Order catch clauses from most to least specific.
  • Mastery of core invariant: Explain why throwing across raw owning pointers leaks.
  • Hardware and ABI awareness: Explaining how sizes, alignments, calling conventions, or kernel boundaries behave on real 64-bit targets rather than relying on implementation-defined assumptions.
  • Defensive engineering: Naming the exact compiler flags (-Wall -Wextra -Werror), static analyzers, or runtime sanitizers (-fsanitize=address,undefined,thread) that verify correctness in CI.

Common mistakes

  • Explaining only surface syntax without tracing the underlying memory addresses, ownership lifetime, or kernel/user-space state transitions.
  • Assuming undefined behavior (such as signed overflow, uninitialized reads, strict-aliasing violations, or dangling pointers) will "just work" because it passed an unoptimized -O0 debug build.
  • Ignoring return codes, partial reads/writes, alignment padding, or cleanup paths when an allocation or syscall fails midway through a function.

This answer walks

Practice follow-ups

  1. 01Why does throwing from a destructor during unwinding end the program?

Not started

Sign in to save your learning progress.

Sign in to save