c++exception

How to catch the Control-C exception in a Visual C++ console app?


This seems like it ought to be simple, but I've tried both
try {} catch (...) {} C++ exception handling and
__try {} __finally {} structured exception handling (SEH)
and neither one will catch the exception that happens when you Control-C the application.

I didn't really expect C++ exception handling to do this, since the Control-C is a system-type signal and not caused by a C++ throw(), but I tried it anyway when SEH didn't work.

If I run under a debugger, it shows that Control-C raises a first-chance exception, but when it's rethrown, my handler is never invoked.


Solution

  • Here is how to handle an interrupt in Win32: SetConsoleCtrlHandler [Microsoft Learn].

    Microsoft also provides example code.