1

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


编辑: 对不起 - 我之前省略了一些细节:

语言:C# 2.0

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

4

2 回答 2

2

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

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

    protected void Application_Error(Object sender, EventArgs e)

免责声明:我不是 c++ 开发人员,但从我读到的内容来看,这应该回答你的问题。

于 2008-10-29T14:11:09.840 回答
0

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

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)
{
    ....
}
于 2012-07-26T09:31:40.180 回答