如何解释Exception是否会捕获RuntimeException? [英] How to explain whether Exception will catch RuntimeException?

查看:118
本文介绍了如何解释Exception是否会捕获RuntimeException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这对我来说很奇怪. RuntimeException 继承自 Exception ,后者继承自 Throwable .

This is very odd to me. RuntimeException inherits from Exception, which inherits from Throwable.

catch(Exception exc) { /* won't catch RuntimeException */

但是

catch(Throwable exc) { /* will catch RuntimeException */

我知道 RuntimeException 的特殊之处在于它未经检查.但据我了解,这仅适用于是否必须声明异常,而不是是否捕获到异常.即便如此,我也不知道为什么这种逻辑在捕获 Throwable 时会中断.

I know RuntimeException is special in that it's unchecked. But to my understanding that applies just to whether exceptions have to be declared, not whether they are caught. And even then, I don't know why this logic would break on catching Throwable.

这与我非常相关,因为我有一种情况,可以在终端操作中抛出 RuntimeException .我不确定该模式的名称,但是类似我的类 EmailRoller 会使用一组 Callbacks .代码如下:

This is pretty relevant to me since I have a situation where RuntimeException can be thrown in a terminal operation. I'm not sure the name for this pattern, but something like, my class EmailRoller takes an array of Callbacks. The code looks like this:

for(Callback cb : callbacks) {
    try {
        cb.call(item);
    }
    catch(Exception exc) {
        logger.error("Error in callback: ", exc);
   }
}

因此,在这种情况下,诸如OOME之类的数据就需要通过,因为如果这些回调之一消耗了所有机器内存,那么肯定会影响其他回调的运行.但是 NullPointerException 吗?还是 IndexOutOfBoundsException ?这些会影响回调,但不会阻止其他回调运行.

So this is a case where something like an OOME needs to fly through, because if one of these callbacks consumes all machine memory, that sure as heck is going to affect the running of the other ones. But a NullPointerException? Or an IndexOutOfBoundsException? Those will affect the callback but won't prevent the others from running.

此外,这有点像企业设计.不同的程序员或团队可以添加回调来处理项目,但应将它们彼此隔离.这意味着,作为负责使这些回调彼此隔离的程序员,我不应该依赖它们来确保不会漏掉错误.捕获 Exception 应该在正确的位置,但这不是因为 RuntimeException 通过.所以我更笼统的问题是:这里有什么好的模式?只是 catch(Exception | RuntimeException exc),我认为由于继承而导致语法错误?

Also, this is a bit of an enterprise design. Different programmers or teams can add callbacks to process the item, but they should be isolated from each other. This means, as the programmer responsible for insulating these callbacks from each other, I shouldn't rely on them to make sure errors don't slip through. Catching Exception should be about the right line, but it isn't because RuntimeException slips through. So my more general question is: what's a good pattern here? Just catch(Exception | RuntimeException exc), which I believe is a syntax error because of the inheritance?

推荐答案

问题的前提是有缺陷的,因为捕获 Exception 确实捕获 RuntimeException .演示代码:

The premise of the question is flawed, because catching Exception does catch RuntimeException. Demo code:

public class Test {
    public static void main(String[] args) {
        try {
            throw new RuntimeException("Bang");
        } catch (Exception e) {
            System.out.println("I caught: " + e);
        }
    }
}

输出:

I caught: java.lang.RuntimeException: Bang

在以下情况下,您的循环将出现问题:

Your loop will have problems if:

  • 回调为空
  • 在循环执行时(如果是集合而不是数组),任何东西都会修改回调

也许这就是您所看到的?

Perhaps that's what you're seeing?

这篇关于如何解释Exception是否会捕获RuntimeException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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