未处理的异常处理程序的异常调度如何变化?

How does exception dispatching change with an unhandled exception handler?

简而言之,MSDN describes exception dispatching 对于这样的用户模式应用程序:

  1. 调试器收到第一次机会异常的通知(如果附加)
  2. 又名异常处理程序。 try/catch 被调用(如果可用)
  3. 调试器收到第二次机会异常的通知(如果附加)
  4. 系统关心未处理的异常(通常:终止进程)

此序列不考虑未处理的异常处理程序的存在。当存在未处理的异常处理程序时,异常调度如何变化?

未处理的异常处理程序插入位置 3。顺序为:

  1. 调试器收到第一次机会异常的通知(如果附加)
  2. 又名异常处理程序。 try/catch 被调用(如果可用)
  3. 调用未处理的异常处理程序(注意复数)(如果可用)
  4. 调试器收到第二次机会异常的通知(如果附加)
  5. 系统关心未处理的异常(通常:终止进程)

下面的 C# 程序对此进行了演示。根据 .NET 版本,您将收到另一个未处理的异常处理程序的消息,这是 .NET 框架打印异常和调用堆栈。

using System;
namespace UnhandledException
{
    static class Program
    {
        static void Main()
        {
            Console.WriteLine("Please attach the debugger now and press Enter.");
            Console.ReadLine();            
            AppDomain.CurrentDomain.UnhandledException += (sender, e) => Unhandled1();
            AppDomain.CurrentDomain.UnhandledException += (sender, e) => Unhandled2();
            try
            {
                Console.WriteLine("Throwing now.");
                // Will cause a first chance, because in try/catch
                throw new Exception("Any exception will do");
            }
            catch (Exception)
            {
                // Will cause first chance and second chance, because in NOT try/catch
                Console.WriteLine("In catch block.");
                throw;
            }
        }

        static void Unhandled1() => Console.WriteLine("In unhandled exception handler 1");
        static void Unhandled2() => Console.WriteLine("In unhandled exception handler 2");
    }
}

调试器 (WinDbg) 中所需的命令:

.symfix
.reload
sxe clr
g; *** for the breakpoint due to attaching the debugger
g; *** first chance in try/catch
g; *** first chance outside try/catch
g; *** second chance