在MFC应用程序中,最上面的try/catch放在哪里? [英] In a MFC application, where to put a topmost try/catch?

查看:317
本文介绍了在MFC应用程序中,最上面的try/catch放在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MFC应用程序中,最上面的try/catch放在哪里?

In a MFC application, where to put a topmost try/catch?

我有一个MFC应用程序,我想捕获所有异常并显示自己的消息框.

I have a MFC application and I would like to catch all the exceptions and show my own message box.

这是我的最高try/catch块的想法:

This is my idea for a topmost try/catch block:

try
{
   // What enclose here? Or, where to put this try/catch block?
}
catch( const std::exception& e )
{
   ::MessageBox(0,e.what(),"I do not know hot to handle this exception, I will terminate",MB_OK);
}
catch(...)
{
   ::MessageBox(0,"Unknown Excpetion","I do not know hot to handle this exception, I will terminate",MB_OK);
}
::TerminateProcess( ::GetCurrentProcess(), -1 );

但是,我可以在哪里放置积木? 我使用Visual Studio 2010创建了一个基于MFC对话框的应用程序,并在Windows 7上的Release x64中对其进行了编译. 我在OnTimer方法中抛出std::exception(将字符串传递给构造函数),没有该块,我得到了csrss.exe创建的带有此通用消息

But, where can I put the block? I created a MFC dialog based application with Visual Studio 2010, and compiled it in Release x64, I am on Windows 7. I throw a std::exception (passing a string to the constructor) in an OnTimer method and without the block I get a message box created by csrss.exe with this generic message

异常未知软件异常(0x40000015)发生在 位置为0x5dff61c9的应用程序."

"The exception unknown software exception (0x40000015) occurred in the application at location 0x5dff61c9."

单击确定"以终止程序"

"Click on OK to terminate the program"

单击取消"以调试程序"

"Click on CANCEL to debug the program"

该消息框未报告我附加到异常的字符串,因此它不是那么有用. 我想我得到的是消息框,而不是精美的TaskDialog,因为我禁用了Windows错误报告服务,并重命名了WerFault.exe.

The message box does not report the string I attached to the exception and so it is not so useful. I think I get the message box instead of a fancy TaskDialog because I disabled the Windows Error Reporting Service and renamed the WerFault.exe.

也许我必须忘记自己的消息框,并且需要接受新的Windows错误报告?

Maybe I have to forget my own message box and I need to embrace the new Windows Error Reporting?

推荐答案

在MFC应用程序中处理未处理的异常的正确方法是通过覆盖

The correct way to process unhandled exceptions in an MFC application is by overriding CWinApp::ProcessWndProcException

您可能只想处理某些异常类型.如果要在某些情况下恢复默认行为,请调用基本实现.如果您不致电该基地,您的应用程序将不会关闭.

You may want to only handle certain exception types. If you want to fall back on the default behavior in some circumstances, call the base implementation. If you do not call the base, your app will not shut down.

如果要显示自定义错误消息,然后在避免出现默认消息的情况下关闭它,请显示消息框,然后在主框架/对话框上调用DestroyWindow.

If you want to display a custom error message and then shut down while avoiding the default message, display your message box and then call DestroyWindow on your main frame/dialog.

这篇关于在MFC应用程序中,最上面的try/catch放在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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