【问题标题】:.Net Equivalent of set_unexpected().Net 等价于 set_unexpected()
【发布时间】:2008-10-29 13:58:17
【问题描述】:

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


编辑:抱歉——我之前省略了一些细节:

语言:C# 2.0

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

【问题讨论】:

  • 您能否详细说明您想要达到的总体效果?很有可能 .NET 中的惯用方式不会完全反映意外/set_unexpected。
  • 是的,乔恩,我明白你在说什么。在我发布我的问题后,我意识到我确实在细节上有所吝啬。我会适当地编辑。

标签: c# .net exception-handling .net-2.0 unhandled


【解决方案1】:

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

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

    protected void Application_Error(Object sender, EventArgs e)

免责声明:我不是 c++ 开发人员,但根据我阅读的内容,这应该可以回答您的问题。

【讨论】:

    【解决方案2】:

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

    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)
    {
        ....
    }
    

    【讨论】:

      猜你喜欢
      • 2010-11-27
      • 2010-11-06
      • 1970-01-01
      • 1970-01-01
      • 2011-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多