c# - 在 C# 中,try-finally 如何捕获原始异常

标签 c# .net exception try-catch try-finally

我的简单例子是:

    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() 方法!

最佳答案

是的,这是可能的,尽管非常讨厌!

一个鲜为人知的事实是,CLR 异常在实际捕获到异常之前不会导致 finally block 的执行。这在某种程度上是伪装的,因为如果没有捕获到异常(并使其脱离 Main),那么 CLR 托管代码的默认行为是为您运行 finally block ,给人一种它们总是在运行的错觉。

但是,有一种方法可以在捕获异常之前检查它,以决定是否要捕获它。试试这个:

static bool StoreFirstException(Exception x, Action<Exception> store)
{
    if (x.Message == "Exception 1")
    {
        store(x);                
    }

    return true;
}

static void Method1()
{
    Exception firstException = null;

    try
    {
        FixedUnalterableMethod(); //line 24.
    }
    catch (Exception ex) when (StoreFirstException(ex, x => firstException = x))
    {
        Console.WriteLine(firstException);               
        Console.WriteLine(ex);
    }
}

catch...when 功能让您可以编写一个 bool 表达式来检查异常。在这里,我检查消息(您给我的唯一区别事实),如果这是第一个异常,我将其传递给 store 操作。

调用者使用此回调来隐藏第一个异常。

然后它投票给 catch,这只会导致 finally block 执行,从而抛出第二个异常。相同的 when 子句会检查它,但这次不会将它提供给 store。因此,我在 catch block 中有两个异常,并将它们都记录下来。我的控制台显示了两个异常以及正确的源代码行号。

这是不查看消息的版本;它只是假设它看到的第一个异常一定是有趣的异常。使用嵌套函数也更简洁:

static void Method1()
{
    Exception firstException = null;

    bool StoreFirstException(Exception x)
    {
        if (firstException == null) firstException = x;
        return true;
    }

    try
    {
        FixedUnalterableMethod(); //line 24.
    }
    catch (Exception ex) when (StoreFirstException(ex))
    {
        Console.WriteLine(firstException);               
        Console.WriteLine(ex);
    }
}

关于c# - 在 C# 中,try-finally 如何捕获原始异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57544142/

相关文章:

c# - ViewModel 中属性的 BindingExpression 路径错误

java - NumberFormatException:对于输入字符串: ""?

Java 输入不匹配异常

ios - @throw NSException 与 NSException 引发

c# - 缺少桌面 DLL 中的 Prism PopupChildWindowAction

c# - Form.Parent 和 StartPosition.CenterParent

c# - 如何在不影响在 C# 中使用它的客户端的情况下更改枚举定义

.net - HttpWebRequest 未发送所有 cookie

.net - 如何从 PowerShell 访问 Web 服务?

c# - IsolatedStorage 不适用于 IE 保护模式? C#