`try / catch`如何工作? [英] How does `try / catch` work in details

查看:167
本文介绍了`try / catch`如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要内部的 try {} catch {} 块和堆栈跟踪的工作方式。

I would like to the innards of how does try {} catch {} block and stack traces work.

我正在阅读这篇关于异常处理反模式的伟大文章,并找到以下段落:

I was reading this great article about exception handling anti-patterns and found the following paragraph:


catch (NoSuchMethodException e) {
  throw new MyServiceException("Blah: " +
      e.getMessage());
}

这会破坏原始异常的堆栈跟踪,并且总是错误的。 / p>

This destroys the stack trace of the original exception, and is always wrong.

之后,我意识到我不真的知道如何 try / catch 的作品。我的理解如下。考虑一下这个例子:

After that I realized that I don't really know how try/catch works. My understanding is the following. Consider the example:

void top() {
    try {
        f();
    } catch (MyException ex) {
        handleIt(); 
    } finally {
        cleanup();
    }
}

void f() {
    g();
}

void g() {
    throw new MyException();
}

当我打电话 top(),呼叫链 top - > f - > g
留下两个堆栈框架调用堆栈(对于顶部 f 函数)。当$ g 中引发异常时,
程序会弹起执行堆栈,直到找到 try / catch 处理异常的块。同时,它释放堆栈帧,并将堆栈跟踪信息附加到一些可以传递给 catch 的魔术对象,并且可以打印堆栈跟踪。

When I call top(), the call chain top -> f -> g leaves two stack frames on the call stack (for top and f functions). When the exception is raised in g, the program bubbles up the execution stack until it finds try/catch block that handles the exception. Meanwhile it frees the stack frames and attach stack trace information to some "magic" object that can be passed to catch and the stack trace can be printed.

它如何知道被调用的函数是被包围的try / catch块?这个信息是否绑定到堆栈帧?像一个指向错误处理块的指针(某些开关选择匹配的 catch 块),并指向 finally 块?为什么 e.getMessage()在上述示例中是破坏性的(见评论)?

How does it know that the called function is "surrounded" with the try/catch block? Is this information bound to the stack frame? Like, a pointer to error handling block (some switch selecting a matching catch block), and a pointer to finally block? Why e.getMessage() is destructive in the example above (see the comments)?

注意, em>知道如何使用try / catch和异常,我想知道它如何工作

推荐答案

它如何知道被调用的函数是被包围的try / catch块?

"How does it know that the called function is "surrounded" with the try/catch block?"

每个方法的代码包含异常表,其中描述该方法的所有try-catch块。

The code of each method contains Exception Table which describes all try-catch blocks of that method.

当调用一个过程(函数,方法)时,当前堆栈帧将附加调用指令的地址,以便在正确的指令(下一步调用指令后)恢复该帧的执行。

When a procedure (function, method) is called, the current stack frame is appended with the address of the calling instruction, so as to restore execution of that frame at the correct instruction (next after calling instruction).

当执行throw语句时,JVM 检查每个堆栈框架,以确定该框架是否可以处理该异常。如果它的方法包含一个包含调用指令的try-catch块,并且块的异常类型是抛出的异常的超类型(或相同的)。如果找到这样一个框架,则框架从try-catch块指向的指令中恢复其执行。

When a throw statement is executed, the JVM examines each stack frame to find out if that frame can handle the exception. It can if its method contains a try-catch block which contains the calling instruction, and the type of block's exception is a supertype (or the same as) of the thrown exception. If such a frame is found, the frame restores its execution from the instruction pointed to from the try-catch block.

这篇关于`try / catch`如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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