如果我会这样写:
void Code()
{
Console.WriteLine("a");
throw new Exception(); //the code will stop executing here
Console.WriteLine("b");
}
代码将在将“a”写入控制台后停止执行。我想知道,是否可以执行整个函数然后抛出异常?所以如果在 Console.WriteLine("a"); 之间会抛出异常。和 Console.WriteLine("b");,它将停止代码执行,但我希望在抛出异常之前完全执行函数
最佳答案
完全没有建议,但我想你可以这样做。
var errors = new List < Exception > ();
Console.WriteLine("a");
try {
ErrorThrowingMethod();
} catch (Exception e) {
errors.Add(e);
}
Console.WriteLine("b");
if (errors.Any()) throw new AggregateException(errors);
关于c# - 禁止在异常时中断代码执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29168947/