C# 确定异常是否用消息初始化

C# Determinig if exception initialized with a message

有没有办法在捕获异常时确定它是否是用非默认消息构造的。

        try
        {
            throw new Exception(message);      // case 1
            //throw new Exception();           // case 2
        }

        catch(Exception exp)
        {
            /* what do I put here such that if the case 2 exception were
               caught it would output exp.ToString() instead of exp.Message? */

            textBox1.Text = exp.Message;  // case 1 handeling

        }

只是为了说明什么时候抛出 Exception(message) 我希望它输出 exp.Message 并且当 Exception() 抛出时我想要输出 exp.ToString()。我宁愿在不添加自定义异常的情况下完成此操作。谢谢

您需要根据默认异常检查邮件

catch (Exception e)
{
  bool isDefaultMessage = e.Message == new Exception().Message;
}

更新

不同类型的异常

catch (Exception e)
{
  bool isDefaultMessage = false;
  try
  {
     var x = (Exception) Activator.CreateInstance(e.GetType());
     isDefaultMessage = e.Message == x.Message;
  }
  catch (Exception) {} // cannot create default exception.
}