我编写了简单的 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 必须运行,那么解决方案就是精确地执行您在第二个代码段中所做的操作:处理未处理的异常。