【问题标题】:Display Exception on try-catch clause在 try-catch 子句上显示异常
【发布时间】:2013-04-15 06:05:53
【问题描述】:

到目前为止,每当我想显示我使用的代码抛出的异常时:

try
{
    // Code that may throw different exceptions
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());         
}

我使用上面的代码主要是出于调试原因,以便查看异常的确切类型以及引发异常的相应原因。

在我现在创建的一个项目中,我使用了几个try-catch 子句,并且我想在出现异常时显示一个弹出消息,以使其更加“用户友好”。通过“用户友好”,我的意思是一条消息会隐藏诸如 Null Reference ExceptionArgument Out Of Range Exception 之类的短语,这些短语当前与上述代码一起显示。

但是,我仍然希望查看与创建消息的异常类型相关的信息。

有没有办法根据之前的需要来格式化抛出异常的显示输出?

【问题讨论】:

  • 您可以显示任何类型的消息,或以多种方式对异常做出反应,一旦您发现异常,您就可以控制。
  • 如果你要调试,为什么不直接使用调试器呢?如果最终用户发生任何意外异常,只需将其记录下来并让程序终止。
  • 最好为每个异常添加一条消息,而不是仅仅 1..添加多个 catch 块并在每个中添加 msgbox。不要直接捕获异常
  • 我现在有问题...你们都提供了有用的答案,我不知道选择哪一个是正确的。基本上 ex.toString() 和 ex.Message 之间的区别几乎每个人都提到的是有帮助的想法

标签: c# .net exception try-catch


【解决方案1】:

Exception.Message 提供比Exception.ToString() 更(但不完全)用户友好的消息。考虑这个人为的例子:

try
{
    throw new InvalidOperationException();
}
catch(InvalidOperationException ex)
{
    Console.WriteLine(ex.ToString());
}

虽然Message 产生的消息比ToString() 更简单,但显示的消息对用户来说仍然没有多大意义。手动吞下异常并向用户显示自定义消息以帮助他们解决此问题并不需要太多努力。

try
{
    using (StreamReader reader = new StreamReader("fff")){}
}
catch(ArgumentException argumentEx)
{
    Console.WriteLine("The path that you specified was invalid");
    Debug.Print(argumentEx.Message);

}
catch (FileNotFoundException fileNotFoundEx)
{
    Console.WriteLine("The program could not find the specified path");
    Debug.Print(fileNotFoundEx.Message);
}

您甚至可以使用Debug.Print 将文本输出到即时窗口或输出窗口(取决于您的 VS 偏好)以进行调试。

【讨论】:

    【解决方案2】:

    您可以使用Exception.Message 属性来获取描述当前异常的消息。

      catch (Exception ex)
       {
         MessageBox.Show(ex.Messagge());         
       }
    

    【讨论】:

      【解决方案3】:
      try
           {
              /////Code that  may throws several types of Exceptions
           }    
           catch (Exception ex)
             {
               MessageBox.Show(ex.Message);         
             }
      

      使用上面的代码。

      还可以将自定义错误消息显示为:

      try
           {
              /////Code that  may throws several types of Exceptions
           }    
           catch (Exception ex)
             {
               MessageBox.Show("Custom Error Text "+ex.Message);         
             }
      

      补充:

      ex.toString() 和 ex.Message 的区别如下:

      Exception.Message vs Exception.ToString()

      所有细节与例子:

      http://www.dotnetperls.com/exception

      【讨论】:

      • MessageBox.Show(ex.Message);
      【解决方案4】:

      您可以使用.Message,但我不建议直接使用Exception。尝试捕获多个异常或显式声明异常并将错误消息定制为异常类型。

      try 
      {
         // Operations
      } 
      catch (ArgumentOutOfRangeException ex) 
      {
         MessageBox.Show("The argument is out of range, please specify a valid argument");
      }
      

      捕获Exception 是相当通用的,可能被视为不好的做法,因为它可能会隐藏应用程序中的错误。

      您还可以通过检查异常类型来检查异常类型并进行相应的处理:

      try
      {
      
      } 
      catch (Exception e) 
      {
         if (e is ArgumentOutOfRangeException) 
         { 
            MessageBox.Show("Argument is out of range");
         } 
         else if (e is FormatException) 
         { 
            MessageBox.Show("Format Exception");
         } 
         else 
         {
            throw;
         }
      }
      

      如果异常是 ArgumentOutOfRange 或 FormatException,它将向用户显示一个消息框,否则它将重新抛出异常(并保留原始堆栈跟踪)。

      【讨论】:

      • 您忘记了最后一个需要再次throw; 异常的情况。
      • 在您的第二个示例中,您可以使用throw; 以便在 try 链中进一步传播它
      • ArgumentOutOfRangeExceptionFormatException写几个catch子句有什么问题,而不是在一个catch块中写if else语句
      • @IlyaIvanov - 我会推荐前者,因为我认为它更清洁 IMO。我在演示您可以检查 Exception 的类型(如果您捕获了通用 Exception 类型),但是我的主要观点是,我宁愿明确声明要捕获的类型,而不是直接捕获 Exception
      • 好的,同意,我只是觉得最好提前教一下如何正确组织你的 catch 块。
      【解决方案5】:

      试试这个代码:

            try
            {
              // Code that may throw different exceptions
            }
            catch (Exception exp)
            {
                 MessageBox.Show(exp.Message());         
            }
      

      【讨论】:

        【解决方案6】:

        诀窍是使用异常的Message方法:

        catch (Exception ex)
        {
            MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        

        【讨论】:

          猜你喜欢
          • 2014-09-09
          • 1970-01-01
          • 2023-03-25
          • 2011-09-16
          • 1970-01-01
          • 1970-01-01
          • 2016-05-08
          • 2011-04-01
          • 1970-01-01
          相关资源
          最近更新 更多