c# - 最后抛出异常。 CLR 行为与 try-catch block

标签 c# .net exception try-catch clr

<分区>

我编写了简单的 C# 控制台应用程序:

class Mystery
{
    static void Main(string[] args)
    {
        MakeMess();
    }

    private static void MakeMess()
    {
        try
        {
            System.Console.WriteLine("try");
            throw new Exception(); // let's invoke catch
        }
        catch(Exception)
        {
            System.Console.WriteLine("catch");
            throw new Exception("A");
        }
        finally
        {
            System.Console.WriteLine("finally");
            throw new Exception("B");
        }
    }
}

控制台的输出是:

try

catch

Unhandled Exception: System.Exception: A at Mystery.Program.MakeMess() in ...

似乎 CLR 捕获了 A 并且根本没有调用 finally block 。

但是当我用 try-catch block 包围对 MakeMess() 的调用时:

static void Main(string[] args)
{
    try
    {
        MakeMess();
    }
    catch(Exception ex)
    {
        System.Console.WriteLine("Main caught " + ex.Message);
    }
}

输出看起来完全不同:

try

catch

finally

Main caught B

似乎在方法外严格处理异常时,从 MakeMess() 传播的异常是不同的。

这种行为的解释是什么?

最佳答案

您看到的行为与是否抛出 finally block 无关。您只是在您的应用程序中有一个未处理的异常,当这种情况发生时,所有的赌注都会关闭,包括 finally block 是否运行:

来自 MSDN文档:

Within a handled exception, the associated finally block is guaranteed to be run. However, if the exception is unhandled, execution of the finally block is dependent on how the exception unwind operation is triggered. That, in turn, is dependent on how your computer is set up. For more information, see Unhandled Exception Processing in the CLR.

Usually, when an unhandled exception ends an application, whether or not the finally block is run is not important. However, if you have statements in a finally block that must be run even in that situation, one solution is to add a catch block to the try-finally statement. Alternatively, you can catch the exception that might be thrown in the try block of a try-finally statement higher up the call stack. That is, you can catch the exception in the method that calls the method that contains the try-finally statement, or in the method that calls that method, or in any method in the call stack. If the exception is not caught, execution of the finally block depends on whether the operating system chooses to trigger an exception unwind operation.

如果 finally block 必须运行,那么解决方案就是精确地执行您在第二个代码段中所做的操作:处理未处理的异常。

关于c# - 最后抛出异常。 CLR 行为与 try-catch block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43802728/

相关文章:

.net - 更改 log4net 配置以每 x 小时创建一个新日志

c# - xamarin.ios mp3 在屏幕关闭时停止播放

c# - 为什么在 Dynamics 365 XRM 工具 SDK 中使用 IOrganizationService 而不是 CrmServiceClient?

C# - 不同的 .NET 框架版本

c# - 如何在 Excel 共享插件中使用对话框

c++ - 异常消息 : Insert String Representation of Faulty Value

c# - 在图片框内创建矩形绘图

c# - 具有通用功能的接口(interface)

java - 编译时错误: illegal Character:\8279

java - 跟踪程序中的所有异常