【发布时间】:2015-12-14 22:47:53
【问题描述】:
有没有办法在捕获异常时确定它是否是用非默认消息构造的。
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
}
只是为了澄清什么时候抛出异常(消息),我希望它输出 exp.Message,而当抛出异常()时,我想输出 exp.ToString()。我宁愿在不添加自定义异常的情况下完成此操作。谢谢。
【问题讨论】:
-
在上面的示例中,如果 throw Exception(),则 exp.Message 的值显示为“抛出了 'System.Exception' 类型的异常。”
标签: c# exception constructor try-catch throw