2012-02-08 41 views
1

我有用户获取Windows XP错误报告“[我的应用程序]遇到错误,需要关闭”消息。此消息表明我的应用程序中存在未处理的异常?我有AppDomain.Unhandlled异常连接起来处理我的代码中的任何未捕获的异常,所以我不知道用户如何收到此错误。对类似问题的回答说了一些有关单独线程的原因,但这些线程仍然运行在同一个应用程序域中,因此它们将被处理吗?这里是我的处理代码,以防万一你发现一个地方,它抛出一个异常:我怎样才能得到“[MyApp]遇到问题,需要关闭”消息与AppDomain.UnhandledException处理?

public void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) 
    { 
     #region Global Exception Handling 
     string support = String.Empty; 

     //try to get the support information from the common project 
     try 
     { 
      support = Common.Constants.ErrorMessages.RETRY_OR_CONTACT_SUPPORT; 
     } 
     catch(Exception) 
     { 
      //Use hard coded support info if the common project is not accessible 
      support = "Please try again or contact the Support Center at 877-555-5555"; 
     } 

     string message = "An error occured that OASIS was not able to recover from. " + support; 

     try 
     { 
      //Try to use the OasisErrorDialog to show the error. If the exception derives from Exception, use OasisErrorDialog to log it. 
      if (e.ExceptionObject is Exception) 
       OasisErrorDialog.ShowErrorDialog(message, "A Serious Error Has Occured", true, true, (Exception)e.ExceptionObject); 
      else 
      { 
       //The exception doesn't derive from Exception so we need to try to log it using StepUp 
       OasisErrorDialog.ShowErrorDialog(message, "A Serious Error Has Occured"); 
       ExceptionHandler.HandleException("An unhandled error that does not derive from the Exception class was thrown." + e.ExceptionObject.ToString()); 
      } 
     } 
     catch (Exception) 
     { 
      //The OasisErrorDialog wasn't available so display the error in a standard MessageBox 
      MessageBox.Show(message, "A Serious Error Has Occured"); 

      try 
      { 
       //If the StepUp framework is still working, log the exception info 
       if (e.ExceptionObject is Exception) 
        ExceptionHandler.HandleException("An unhandled exception occured", (Exception)e.ExceptionObject); 
       else 
        ExceptionHandler.HandleException("An unhandled error occured: "+e.ExceptionObject.ToString()); 
      } 
      catch (Exception) { } 
     } 
     #endregion 
    } 
+0

嗯,代码不会调用Environment.Exit(),所以这是正常的。 – 2012-04-04 17:41:25

+0

Environment.Exit()在这种情况下通常会被调用吗?我认为捕捉例外会阻止它进一步提升。虽然我确实打算让应用程序停止运行。 – xr280xr 2012-04-11 18:58:53

回答

0

如果你的应用做了很可怕(例如错误的互操作导致内存破坏),那么你不会看到一个例外。你的应用程序将会失败。

稍微不太可怕的事情,如内存不足也可能导致您的应用程序失败,但不运行异常处理程序。

This question and its answers讨论捕捉所有例外的不可能性。

+0

谢谢。所以.net异常的“正常”(缺少更好的分类)类型应该通过处理程序,而不管它发生在正确的位置?当用户报告崩溃时正在使用的代码中充斥着异常处理,所以我不确定目前发生了什么。我知道存在数据库连接问题,并且有一些无关的后台线程正在运行,这就是为什么我会问。 – xr280xr 2012-02-08 18:46:40

+0

只要您只有一个应用程序域,您应该看到所有“正常”异常。 – arx 2012-02-08 18:58:18