Skip to content

Catching any exception from a code block

Question

In C++, how do you write a handler that catches every possible exception thrown inside a try block?

Short answer

Use the catch-all handler catch (...). Place it after any more specific handlers (such as catch (const std::exception& e)). Inside it you cannot inspect the exception, but you can clean up and rethrow it unchanged with a bare throw;, or capture it with std::current_exception().

Adapted from Joshi, Denson & Downes, Quant Job Interview Questions and Answers, Q7.34, p.233; question and answer rewritten by SelfMadeQuant.com.

See the hint and the full worked solution in the question bank (free account).

Open the worked solution

Lessons behind this question