【问题标题】:Catching all exceptions in Runtime? C# [duplicate]在运行时捕获所有异常? C# [重复]
【发布时间】:2013-09-24 06:58:48
【问题描述】:

我希望能够捕获程序的每个异常并将其显示在 MessageBox 中,而不是让程序只说“已停止工作”。

出于某种原因 - 每次软件出现故障时 - 程序都会说停止工作。我希望能够在 MessageBox 中显示它,就像在 Visual Studio 中一样。怎么可能?

C# WinForms。

【问题讨论】:

  • 如果异常是错误,则修复错误,使程序不再抛出。如果异常是由于找不到文件等外生条件引起的,则捕获异常并处理它们。
  • @EricLippert 有时我必须在我的 VPS 而不是我的个人计算机中测试代码。当异常发生时 - 我希望能够看到原因和位置。
  • 在这种情况下,您只会收到一条看起来更好看的消息,而没有真正的信息可以帮助您,您需要查看某种日志记录来告诉您错误发生的位置/原因
  • 这实际上是在“调试”发布正在部署的软件时非常重要的问题。幸运的是,windows 还记录了所有未捕获的异常。信息中心什么的。抱歉含糊不清,已经有一段时间了。

标签: c# exception


【解决方案1】:

订阅 ThreadException 和 CurrentDomain.UnhandledException

static void Main(){
    Application.ThreadException += ApplicationThreadException;
    AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;
}
static void ApplicationThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
    ShowGenericErrorMessage();
}
static void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    ShowGenericErrorMessage();
}

【讨论】:

    【解决方案2】:

    Global.asax 中的 Application_Error 方法是你抓住的最后机会:

    protected void Application_Error(Object sender, EventArgs e)
    

    【讨论】:

      【解决方案3】:

      试试类似的东西:

      public Form1()
              {
                  InitializeComponent();
                  AppDomain.CurrentDomain.UnhandledException += HandleUnhandledException;
              }
      
              private void HandleUnhandledException(object sender, UnhandledExceptionEventArgs e)
              {
                  MessageBox.Show("Exception {0} was thrown", e.ToString());
              }
      

      【讨论】:

        【解决方案4】:

        试试这个:

        try
        {
        \\Code block
        }
        
        catch(Exception ex)
        {
        \\the object ex has details about the exception use it display the error in msg box
        }
        

        此外,此链接简单地解释了异常处理:http://www.dotnetperls.com/exception

        【讨论】:

          【解决方案5】:

          创建一个未处理的异常处理程序,如下所示:

          static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs args)
          {
              Exception ex = (Exception)args.ExceptionObject;
          
              UtilGui.LogException(ex);
          }
          
          static void ApplicationThreadUnhandledExceptionHandler(object sender, System.Threading.ThreadExceptionEventArgs args)
          {
              Exception ex = (Exception)args.Exception;
          
              UtilGui.LogException(ex);
          }
          

          并在您的 Main 方法中注册它,如下所示:

          // Add the event handler for handling UI thread exceptions to the event.
          Application.ThreadException += new ThreadExceptionEventHandler(ApplicationThreadUnhandledExceptionHandler);
          
          // Set the unhandled exception mode to force all Windows Forms 
          // errors to go through our handler.
          Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
          
          // Add the event handler for handling non-UI thread exceptions to the event.
          AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomainUnhandledExceptionHandler);
          

          【讨论】:

            猜你喜欢
            • 2012-09-01
            • 2011-11-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-11-02
            • 1970-01-01
            • 1970-01-01
            • 2016-05-28
            相关资源
            最近更新 更多