是否有.net等效的C ++ unexpected()/ set_unexpected()功能?


修改抱歉 - 之前我省略了一些细节:

语言:C#2.0

我有一些遗留应用似乎在某处抛出了一些未处理的异常。我只是想放置一些东西来阻止客户的痛苦,直到我能够找到问题的实际来源。在C ++中,set_unexpected()指向的函数,据我所知,当一个未处理的异常冒泡到主例程时会被调用。因此我的问题是.net等效功能。

有帮助吗?

解决方案

根据应用程序的类型,有3种处理未处理异常的可能方案:

  1. 对于Windows窗体应用程序,将事件处理程序挂钩到 Application.ThreadException
  2. 对于命令行应用程序,将事件处理程序挂钩到 AppDomain。 UnhandledException
  3. 对于ASP.NET应用程序,在Global.asax中,创建:

    protected void Application_Error(Object sender,EventArgs e)

  4. 免责声明:我不是c ++开发人员,但根据我的阅读,这应该回答你的问题。

其他提示

这些处理程序应该在混合模式应用程序中捕获大多数意外异常。

private delegate long UnhandledExceptionFilter(IntPtr exception);

[DllImport("KERNEL32.DLL", SetLastError = true)]
private static extern IntPtr SetUnhandledExceptionFilter([MarshalAs(UnmanagedType.FunctionPtr)] UnhandledExceptionFilter filter);

// put these in your bootstrapper
AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;
Application.ThreadException += ApplicationThreadException;
SetUnhandledExceptionFilter(UnhandledExceptionFilter);

void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    ...
}

void ApplicationThreadException(object sender, ThreadExceptionEventArgs e)
{
    ...
}

long UnhandledExceptionFilter(IntPtr exception)
{
    ....
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top