为什么在C ++中出现第一次chace异常? [英] why getting first chace exception in c++?

查看:79
本文介绍了为什么在C ++中出现第一次chace异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个示例,以检查在Windows控制台应用程序中按ctrl + C时会发生什么情况:

i made a sample to check what happen when i press ctrl+C in windows console application:

bool    TerminationFlag=true;
int main()
{
g_hTerminateEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL);
::SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
while(1)
    {
if(TerminationFlag==false)
        {
            break;
        }
}
return 0;
}

BOOL WINAPI ConsoleCtrlHandler(DWORD dwCtrlType)
{

    if (dwCtrlType == CTRL_C_EVENT ||
        dwCtrlType == CTRL_BREAK_EVENT ||
        dwCtrlType == CTRL_CLOSE_EVENT)
    {
    TerminationFlag=false;
::SetEvent(g_hTerminateEvent);
return TRUE;
    }
return FALSE;
}





i tested the code by running it using start debugging option in visual studio when i press ctrl+c i get the following message

First-chance exception at 0x7c87647d when i press on continue option my code comes to the line TerminationFlag=false;


即使我已经在控件处理程序中处理了ctrl + c,您能否告诉我问题出在哪里?


even though i have handled ctrl+c in control handler.Can you please tell me whats the problem?

推荐答案

它在MSDN [
如果正在调试控制台进程并且尚未禁用CTRL + C信号,则系统将生成DBG_CONTROL_C异常.仅出于调试器的利益而引发此异常,并且应用程序永远不应使用异常处理程序来处理它.如果调试器处理该异常,则应用程序将不会注意到CTRL + C,但有一个例外:可警报的等待将终止.如果调试器在未处理的情况下传递了异常,则如上所述,将CTRL + C传递到控制台进程并将其视为信号.
It is right in the MSDN[^]. See the Remarks section close to the end.

If a console process is being debugged and CTRL+C signals have not been disabled, the system generates a DBG_CONTROL_C exception. This exception is raised only for the benefit of the debugger, and an application should never use an exception handler to deal with it. If the debugger handles the exception, an application will not notice the CTRL+C, with one exception: alertable waits will terminate. If the debugger passes the exception on unhandled, CTRL+C is passed to the console process and treated as a signal, as previously discussed.


这篇关于为什么在C ++中出现第一次chace异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆