1. 程式人生 > >控制檯程式輸入CTRL+C後彈出異常的原因

控制檯程式輸入CTRL+C後彈出異常的原因

今天在寫一個控制檯程式。按照以前的經驗,輸入CTRL+C後,程式應該正常結束,

但是今天卻每次都彈出一個異常提示框,“0x40010005: Control-C”。

排查了很長時間,終於找到原因了。

實際上這不是什麼錯誤,只要在Debug模式下,輸入CTRL+C或者CTRL+BREAK等命令

都會導致系統內部丟擲一個異常;在非Debug模式下,系統內部不會丟擲這個異常。

參考資料在這裡:

The CTRL+C and CTRL+BREAK key combinations receive special handling by console processes. By default, when a console window has the keyboard focus, CTRL+C or CTRL+BREAK is treated as a signal (SIGINT or SIGBREAK) and not as keyboard input. By default, these signals are passed to all console processes that are attached to the console. (Detached processes are not affected.) The system creates a new thread in each client process to handle the event.

The thread raises an exception if the process is being debugged. The debugger can handle the exception or continue with the exception unhandled.