在 MFC 应用程序中,在哪里放置最顶层的 try/catch?

发布于 2024-12-29 12:16:01 字数 995 浏览 3 评论 0原文

在 MFC 应用程序中,在哪里放置最顶层的 try/catch?

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

这是我对最顶层 try/catch 块的想法:

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 对话框的应用程序,并在 Release x64 中编译了它,我使用的是 Windows 7。 我在 OnTimer 方法中抛出一个 std::Exception(将字符串传递给构造函数),如果没有该块,我会得到一个由 csrss.exe 创建的消息框 带有此通用消息

“异常未知软件异常(0x40000015)发生在 应用程序位于位置 0x5dff61c9。”

“单击“确定”终止程序”

“点击取消调试程序”

消息框不会报告我附加到异常的字符串,因此它不是很有用。 我想我得到的是消息框而不是花哨的任务对话框,因为我禁用了 Windows 错误报告服务并重命名为 WerFault.exe。

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

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

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

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 );

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

"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"

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

表情可笑 2025-01-05 12:16:01

在 MFC 应用程序中处理未处理异常的正确方法是重写 CWinApp::ProcessWndProcException

您可能只想处理某些异常类型。如果您想在某些情况下使用默认行为,请调用基本实现。如果您不呼叫基地,您的应用程序将不会关闭。

如果您想显示自定义错误消息,然后在关闭时避免显示默认消息,请显示消息框,然后在主框架/对话框上调用 DestroyWindow

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文