这是 .NET 的实际来源System.Exception.ToString
:
public override string ToString()
{
return this.ToString(true, true);
}
private string ToString(bool needFileLineInfo, bool needMessage)
{
string str1 = needMessage ? this.Message : (string) null;
string str2 = str1 == null || str1.Length <= 0 ? this.GetClassName() : this.GetClassName() + ": " + str1;
if (this._innerException != null)
str2 = str2 + " ---> " + this._innerException.ToString(needFileLineInfo, needMessage) + Environment.NewLine + " " + Environment.GetRuntimeResourceString("Exception_EndOfInnerExceptionStack");
string stackTrace = this.GetStackTrace(needFileLineInfo);
if (stackTrace != null)
str2 = str2 + Environment.NewLine + stackTrace;
return str2;
}
除了丑陋之外,人们还可以注意到对于所有内部异常,将调用私有的非虚拟 ToString。换句话说,如果你超载ToString
在你的例外中如果你的异常恰好是嵌套的,它不会被调用。哦,等等,事实证明内置异常也有同样的问题,例如System.IO.FileNotFoundException
打印出文件的路径 - 它不是消息的一部分:
public override string ToString()
{
string str = this.GetType().FullName + ": " + this.Message;
if (this._fileName != null && this._fileName.Length != 0)
str = str + Environment.NewLine + Environment.GetResourceString("IO.FileName_Name", new object[1]
{
(object) this._fileName
});
...
}
但是如果你包装一个实例......这个信息将会丢失,除非你自己遍历异常树并检测异常的类型或调用ToString
自己做一些普通的解析。
这是一个恼人的不便,使得记录/写入错误对话框要么丢失信息,要么容易出现错误。有趣的是,Mono 做对了 https://github.com/mono/mono/blob/master/mcs/class/corlib/System/Exception.cs#L317.
.NET版本中是否有什么隐藏的智慧呢?
编辑:这不是基于意见的问题。虽然我觉得这种设计选择很烦人,但我想知道这种方法的好处。了解它们在设计新的解决方案时可能会有所帮助。