【问题标题】:How to "catch" unhandled Exceptions如何“捕获”未处理的异常
【发布时间】:2013-02-18 13:48:37
【问题描述】:

我们开发了一个 .NET 3.5 CF 应用程序,但由于一些 lib 代码中抛出的未处理异常,我们遇到了一些应用程序崩溃。

应用程序终止并显示标准应用程序弹出异常消息框。

有没有办法捕获所有未处理的异常?或者至少,从消息框中捕获文本。我们的大多数客户只是重新启动设备,因此我们无法查看异常消息框。

有什么想法吗?

【问题讨论】:

    标签: compact-framework windows-ce .net-cf-3.5


    【解决方案1】:

    您是否添加了UnhandledException 事件处理程序?

    [MTAThread]
    static void Main(string[] args)
    {
        AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
    
        // start your app logic, etc
        ...
    }
    
    static void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        var exception = (Exception)e.ExceptionObject;
    
        // do something with the info here - log to a file or whatever
        MessageBox.Show(exception.Message);
    }
    

    【讨论】:

    • 太棒了!我将它添加到我的一些项目中,这些项目没有显示任何错误迹象,并且这个东西正在捕获我什至不知道发生的异常。
    【解决方案2】:

    我做的事情类似于ctacke 所做的事情。

    private static Form1 objForm;
    
    [MTAThread]
    static void Main(string[] args)
    {
      objForm = new Form1();
      try
      {
        Application.Run(objForm);
      } catch (Exception err) {
        // do something with the info here - log to a file or whatever
        MessageBox.Show(err.Message);
        if ((objForm != null) && !objForm.IsDisposed)
        {
          // do some clean-up of your code
          // (i.e. enable MS_SIPBUTTON) before application exits.
        }
      }
    }
    

    也许他可以评论我的技术是好是坏。

    【讨论】:

    • 这经常会错过工作线程中的异常和本机异常。 AppDomain.UnhandledException 处理程序往往会捕获更多内容。
    猜你喜欢
    • 2015-09-10
    • 2011-10-27
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 2018-10-19
    • 1970-01-01
    相关资源
    最近更新 更多