c# - 禁止在异常时中断代码执行

标签 c#

如果我会这样写:

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/

相关文章:

c# - DryIoc 与 Serilog - 设置日志记录上下文

c# - 如何使用 C# 将 json 插入到 cosmos db 集合中

c# - moq项目死了吗?我投资学习它是否明智?

c# - 如何使用 LINQ 确保多个集合具有相同的项目计数

c# - 根据相机改变玩家移动的方向

c# - 在 IIS 7 上使用 HttpHandler 取消请求验证

C# 舍入到列表值

c# - 获取数据后关闭EXCEL应用程序进程

c# - 使用 C# 中的互操作将宏插入并运行到 Word 中

c# - Fluent LINQ - 选择包含子列表的父列表,其中子子集存在