15

Up to now, whenever I wanted to show an exception thrown from my code I used:

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

I used the above code mainly for debugging reasons, in order to see the exact type of exception and the according reason the exception was thrown.

In a project I am creating now, I use several try-catch clauses and I would like to display a popup message in case of an exception, to make it more "user friendly". By "user friendly", I mean a message that would hide phrases like Null Reference Exception or Argument Out Of Range Exception that are currently displayed with the above code.

However I still want to see relevant info with the type of exception that created the message.

Is there a way to format the displayed output of thrown exceptions according to previous needs?

4

6 回答 6

13

您可以使用.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,它将向用户显示一个消息框,否则它将重新抛出异常(并保留原始堆栈跟踪)。

于 2013-04-22T11:00:16.270 回答
4
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 与 Exception.ToString()

所有细节与示例:

http://www.dotnetperls.com/exception

于 2013-04-22T10:57:58.090 回答
3

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 首选项)以进行调试。

于 2013-04-22T10:56:54.197 回答
2

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

  catch (Exception ex)
   {
     MessageBox.Show(ex.Messagge());         
   }
于 2013-04-22T10:57:32.343 回答
2

试试这个代码:

      try
      {
        // Code that may throw different exceptions
      }
      catch (Exception exp)
      {
           MessageBox.Show(exp.Message());         
      }
于 2014-04-12T22:27:56.627 回答
1

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

catch (Exception ex)
{
    MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
于 2021-01-21T06:18:26.783 回答