C ++ / CLI:捕获一切(.NET /的Win32 / CRT)异常 [英] C++/CLI: Catching all (.NET/Win32/CRT) exceptions

查看:196
本文介绍了C ++ / CLI:捕获一切(.NET /的Win32 / CRT)异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是令人难以接受的,但我出了选择这里。我正在开发一个C ++ / CLI应用程序,有一个我无法追查错误 - 主要是因为它绕过我目前的崩溃处理程序:

I know this is frowned upon, but I'm out of options here. I'm developing a C++/CLI app that has a bug that I'm unable to track down - mainly because it's bypassing my current crash handler:

AppDomain::CurrentDomain->UnhandledException += gcnew UnhandledExceptionEventHandler(&LogAndExit);
Application::ThreadException += gcnew ThreadExceptionEventHandler(&LogAndExit);
Application::SetUnhandledExceptionMode(UnhandledExceptionMode::CatchException);
try 
{ 
    Application::Run(gcnew frmMain()); 
} 
catch (Exception^ ex) 
{ 
    LogAndExit(ex); 
} 
catch (...) 
{ 
    LogAndExit(); 
}

标准.NET崩溃的处理,我想。 MSDN报道,一些CRT异常将打击在管理堆栈,默默地退出应用程序。

Standard .NET crash handling, I suppose. MSDN reports that some of the CRT exceptions will blow over the managed stack and silently abort the app.

我已经读了_set_invalid_parameter_handler,但即使我得到一个LNK2001错误,现在看来,这不能与/ CLR使用:纯。我说得对,还是我只是PEBKACing起来,缺少一个库文件?

I've been reading up on _set_invalid_parameter_handler, but even though I'm getting a LNK2001 error, it seems it can't be used with /clr:pure. Am I right, or am I just PEBKACing it up and missing a lib file?

推荐答案

你能在运行/ CLR 模式?如果你可以再试试这个:

Can you run in /clr mode? If you can then try this:

#include <exception>

然后:

try
{
    try
    {
        Application::Run(gcnew frmMain()); 
    }
    catch(const exception& ex)
    {
        throw gcnew System::Exception(gcnew System::String(ex.what()));
    }
} 
catch (Exception^ ex) 
{ 
    LogAndExit(ex); 
} 
catch (...) 
{ 
    LogAndExit(); 
}

另外要注意的::如果您的应用程序是多线程的,那么你就只能望尘莫及从线程中 frmMain()正在运行。所以,做一个包罗万象的对整个应用程序是不可能在这种情况下!

Another thing to note: if your application is multi-threaded, then you will only catch an exception from the thread in which frmMain() is running. So doing a catch-all on your entire application is impossible in that case!

这篇关于C ++ / CLI:捕获一切(.NET /的Win32 / CRT)异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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