January 2, 2026
Catch me if you can
Can I throw a C++ exception from a structured exception?
Dev turns OS crashes into C++ drama; commenters clap back
TLDR: A dev tried converting Windows crash errors into C++ exceptions to dodge a slow compiler setting, but it only worked by accident and failed when code was optimized. Commenters debated performance costs and warned: clever hacks won’t beat the rules—use the right setting or accept the risk.
A developer tried a “fiendish plan” to dodge Microsoft’s /EHa flag— a compiler setting that catches both operating system crashes and regular app errors but can slow code. They installed a crash handler that turns a Windows crash into a C++ exception so it could be caught in a try/catch. It looked genius when a printf was inside the try, but removing printf made the catch disappear. Why? The compiler saw nothing inside could throw, optimized away the safety net, and the hack fell flat. The crowd grabbed popcorn.
Then the comments exploded. im3w1l shut down the premise: the slowdown isn’t about catching everything; it’s because any memory touch might explode, forcing variables to stay put. quotemstr flexed: C++ exceptions are already part of Windows’ system, so the answer is “yes/no/obviously.” amelius and rramadass dropped docs and the mixing guide. xerxes901 admitted, “TIL”—surprised the compiler can’t rebuild values mid-crash. Jokes flew about “printf as an exception airbag” and “noexcept means no excuses.” The split: pragmatists say “use /EHa or accept risk,” hackers say “clever tricks or bust.”
Key Points
- •A developer attempted to convert SEH to C++ exceptions by throwing from an unhandled exception filter to avoid using /EHa.
- •With a preceding printf call, the catch block caught the converted exception; without printf, the catch didn’t execute.
- •The first case worked only because printf isn’t noexcept, so the compiler preserved try/catch paths assuming it might throw.
- •When the compiler proves no C++ exceptions can occur (e.g., no potentially-throwing calls), it can optimize away try/catch, breaking the trick.
- •The approach is not a reliable substitute for /EHa, which is required to consistently catch asynchronous (structured) exceptions via C++ handlers.