In a MFC application, where to put a topmost try/c

2019-06-27 16:02发布

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?

1条回答
再贱就再见
2楼-- · 2019-06-27 17:00

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.

查看更多
登录 后发表回答