最后抛出异常。 CLR 行为与 try-catch 块
Exception thrown in catch and finally. CLR behavior vs. try-catch block
我编写了简单的 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 块。
但是当我用 try-catch 块包围对 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() 传播的异常是不同的。
这种行为的解释是什么?
What happens if a finally block throws an exception? :
C# 4 Language Specification § 8.9.5: If the finally block throws another exception, processing of the current exception is terminated.
我认为 finally 块仅用于清理资源,不应抛出任何异常。
您看到的行为与 finally
方块投掷与否无关。您只是在您的应用程序中有一个未处理的异常,当这种情况发生时,所有的赌注都会关闭,包括如果 finally
块 运行 或不:
来自 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 块 必须 运行,那么解决方案就是精确地执行您在第二个代码段中所做的操作:处理未处理的异常。
我编写了简单的 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 块。
但是当我用 try-catch 块包围对 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() 传播的异常是不同的。
这种行为的解释是什么?
What happens if a finally block throws an exception? :
C# 4 Language Specification § 8.9.5: If the finally block throws another exception, processing of the current exception is terminated.
我认为 finally 块仅用于清理资源,不应抛出任何异常。
您看到的行为与 finally
方块投掷与否无关。您只是在您的应用程序中有一个未处理的异常,当这种情况发生时,所有的赌注都会关闭,包括如果 finally
块 运行 或不:
来自 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 块 必须 运行,那么解决方案就是精确地执行您在第二个代码段中所做的操作:处理未处理的异常。