异常调度如何用未处理的异常处理程序进行更改? [英] How does exception dispatching change with an unhandled exception handler?

查看:212
本文介绍了异常调度如何用未处理的异常处理程序进行更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单来说, MSDN描述用户模式应用程序的异常调度,如下所示:


  1. 调试器通知第一次机会异常(如果附加) / li>
  2. 一个异常处理程序。
  3. 调试器被通知第二次机会异常(如果已附加)

  4. 系统关心未处理的异常(通常:终止进程)

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

解决方案

未处理的异常处理程序插入到位置3.序列是:


  1. 调试器通知第一次机会异常(如果附加)

  2. 一个异常处理程序又名。
  3. 未处理的异常处理程序(注意复数)被调用(如果可用) try / catch li>
  4. 调试器通知第二次机会异常(如果附加)

  5. 系统关心未处理的异常(通常:终止进程)

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

 使用系统; 
命名空间UnhandledException
{
静态类程序
{
static void Main()
{
Console.WriteLine(请附加调试器现在并按Enter键。);
Console.ReadLine();
AppDomain.CurrentDomain.UnhandledException + =(sender,e)=> Unhandled1();
AppDomain.CurrentDomain.UnhandledException + =(sender,e)=> Unhandled2();
try
{
Console.WriteLine(现在投掷);
//会导致第一次机会,因为在try / catch
中抛出新的异常(任何异常都会);
}
catch(异常)
{
//将导致第一次机会和第二次机会,因为在NOT try / catch
Console.WriteLine(In catch block 。);
throw;
}
}

static void Unhandled1()=> Console.WriteLine(未处理的异常处理程序1);
static void Unhandled2()=> Console.WriteLine(未处理的异常处理程序2);
}
}

调试器(WinDbg)中需要的命令: p>

  .symfix 
.reload
sxe clr
g; ***因为连接调试器而导致断点
g; ***第一次尝试/抓住
g; ***第一次机会外面尝试/抓住
g; ***第二次机会


In short, MSDN describes exception dispatching for user mode application like this:

  1. the debugger gets notified of the first chance exception (if attached)
  2. an exception handler aka. try/catch is invoked (if available)
  3. the debugger gets notified of the second chance exception (if attached)
  4. the system cares about the unhandled exception (typically: terminate the process)

This sequence does not consider the presence of an unhandled exception handler. How does exception dispatching change when an unhandled exception handler is present?

解决方案

The unhandled exception handlers insert at position 3. The sequence is:

  1. the debugger gets notified of the first chance exception (if attached)
  2. an exception handler aka. try/catch is invoked (if available)
  3. the unhandled exception handlers (note the plural) are called (if available)
  4. the debugger gets notified of the second chance exception (if attached)
  5. the system cares about the unhandled exception (typically: terminate the process)

The following C# program demonstrates it. Depending on the .NET version, you'll a message of another unhandled exception handler, which is the .NET framework printing the exception and the call stack.

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");
    }
}

Commands required in the debugger (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

这篇关于异常调度如何用未处理的异常处理程序进行更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆