【发布时间】:2019-12-23 22:18:52
【问题描述】:
我的简单例子是:
void FixedUnalterableMethod()
{
try
{
throw new Exception("Exception 1"); //line 12.
}
finally
{
throw new Exception("Exception 2"); //line 16.
}
}
void Method1()
{
try
{
FixedUnalterableMethod(); //line 24.
}
catch (Exception ex)
{
var messageWithStackTrace = ex.ToString(); //line 28.
Console.WriteLine(messageWithStackTrace);
}
}
控制台输出为:
System.Exception: Exception 2
at Program.FixedUnalterableMethod() in ...\Program.cs:line 16
at Program.Main(String[] args) in ...\Program.cs:line 24
问题是,如何得知Exception 1 已经发生?
有没有办法在我的 StackTrace 中包含 Exception 1(第 28 行)?
当然我不能修改FixedUnalterableMethod() 方法!
【问题讨论】:
-
不,你不能。抛出异常 2 时,异常 1 被丢弃。
-
为什么要在
finally中抛出异常?你有一个总是想失败的方法,这似乎很奇怪。 -
@John
FixedUnalterableMethod方法在外部 dll 中,我无法更改。但是我想知道Exception 1已经发生了。
标签: c# .net exception try-catch try-finally