如何传递异常来找到处理程序? [英] How is an exception transferred to find a handler?

查看:97
本文介绍了如何传递异常来找到处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当抛出异常时,堆栈展开开始,直到遇到处理代码,但我对整个过程的机制略有不清楚。

When an exception is thrown stack unwinding is initiated until handling code is encountered, but I am a little unclear on the mechanics of the whole process.

1 - 其中是否存储了异常?我不是指实际的异常对象,它可能相当大,例如。有一个消息字符串或某事,但实际的引用或指针,如果你会。它必须是一些统一的存储位置,以便它可以生存下来,因为堆栈正在展开并到达处理位置?

1 - where is the exception stored? I don't mean the actual exception object, which may be quite big, e.g. have a message string or something, but the actual reference or pointer if you will. It must be some uniform storage location so that it can survive going down as the stack is unwinding and reach a handling location?

2 - 程序流程如何确定是否它必须展开特定的功能框架,并调用与程序计数器指示的位置相关联的适当的析构函数或在进一步解开之前寻找异常处理。

2 - how does the program flow determine whether to it has to unwind the particular function frame and call the appropriate destructors associated with the program counter indicated location or seek exception handing before it unwinds further?

3 - 检查什么是被抛出和什么异常正在发生之前?

3 - how is the actual check between what is thrown and what exceptions are being couth happening?

我知道答案可能包括平台特定的东西,在这种情况下,将不胜感激。不需要超越x86 / x64和ARM。

I am aware that the answer might include platform specific stuff, in which case such will be appreciated. No need to go beyond x86/x64 and ARM though.

推荐答案

这些都是实现细节,需要在设计异常处理机制的(非平凡)过程中决定。我只能给出一个可能(或可能不)选择实现这个的草图。

These are all implementation details, to be decided during the (non-trivial) process of designing an exception handling mechanism. I can only give a sketch of how one might (or might not) choose to implement this.

如果你想要一个实现的详细描述,你可以阅读规范适用于GCC和其他流行编译器使用的 Itanium ABI

If you want a detailed description of one implementation, you could read the specification for the Itanium ABI used by GCC and other popular compilers.

1 - 异常对象存储在未指定的位置,必须持续到异常处理完毕。指针或引用在异常处理代码中像任何其他变量一样传递,然后通过一些类似于传递函数参数的机制传递给处理程序(如果它接受引用)。

1 - The exception object is stored in an unspecified place, which must last until the exception has been handled. Pointers or references are passed around within the exception handling code like any other variable, before being passed to the handler (if it takes a reference) by some mechanism similar to passing a function argument.

2 - 有两种常见的方法:将程序位置映射到堆栈帧的信息的静态数据结构;或者包含有关活动处理程序和需要销毁的非平凡堆栈对象的信息的动态堆栈式数据结构。

2 - There are two common approaches: a static data structure mapping the program location to information about the stack frame; or a dynamic stack-like data structure containing information about active handlers and non-trivial stack objects that need destroying.

在第一种情况下,抛出它会看到信息来查看是否有任何本地对象要销毁,以及任何本地处理程序;如果不是,它将在本地堆栈帧上找到函数返回地址,并将相同的进程应用于调用函数的堆栈帧,直到找到处理程序。一旦处理程序被找到,CPU寄存器被更新以引用该堆栈帧,程序可以跳转到处理程序的代码。

In the first case, on throwing it will look at that information to see if there are any local objects to destroy, and any local handlers; if not, it will find the function return address on the local stack frame and apply the same process to the calling function's stack frame until a handler is found. Once the handler is found, the CPU registers are updated to refer to that stack frame, and the program can jump to the handler's code.

在第二种情况下,它会弹出从栈结构,使用它们告诉它如何销毁堆栈对象,直到它找到一个合适的处理程序。一旦处理程序被找到,并且所有解开的堆栈对象被销毁,它可以使用 longjmp 或类似的机制跳转到处理程序。

In the second case it will pop entries from the stack structure, using them to tell it how to destroy stack objects, until it finds a suitable handler. Once the handler is found, and all unwound stack objects destroyed, it can use longjmp or a similar mechanism to jump to the handler.

其他方法是可能的。

3 - 异常处理代码将使用某种数据结构来识别类型,允许它比较类型抛出与处理程序的类型。这是有点复杂的继承;测试不能是一个简单的比较。我不知道任何特定实现的细节。

3 - The exception handling code will use some kind of data structure to identify a type, allowing it to compare the type being thrown with the type for a handler. This is somewhat complicated by inheritance; the test can't be a simple comparison. I don't know the details for any particular implementation.

这篇关于如何传递异常来找到处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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