为什么不捕获异常捕获RuntimeException? [英] Why doesn't catching Exception catch RuntimeException?

查看:594
本文介绍了为什么不捕获异常捕获RuntimeException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对我来说这是非常奇怪的。 RuntimeException 继承自 Exception ,继承自 Throwable 。 / p>

  catch(异常exc){/ *不会捕获RuntimeException * / 
/ pre>

  catch(Throwable exc)将捕获RuntimeException * / 

我知道 RuntimeException 是特别的,它是不加以控制的。但是据我所知,这只适用于是否必须宣布例外情况,而不是是否被捕获。即使如此,我也不知道为什么这个逻辑会捕捉到Throwable。



这与我很相关,因为我有一个可以抛出RuntimeExceptions的情况终端操作。我不知道这个模式的名称,但是我的类 EmailRoller 需要一个回调数组。代码如下所示:

  for(Callback cb:callbacks){
try {
cb。调用(项目);
}
catch(异常exc){
logger.error(回调中的错误,exc);
}
}

所以这是一个像OOME这样的东西需要的情况要飞过,因为如果这些回调中的一个消耗了所有的机器内存,那肯定会影响到其他机器的运行。但是一个 NullPointerException ?或 IndexOutOfBoundsException ?这些将影响回调,但不会阻止其他人运行。



此外,这是一个企业设计。不同的程序员或团队可以添加回调来处理该项目,但它们应该彼此隔离。这意味着,作为负责隔离这些回调的程序员,我不应该依赖它们来确保错误不会通过。捕捉异常应该是正确的一行,但不是因为 RuntimeException 滑过。所以我更普遍的问题是:这里有什么好的模式?只是 catch(Exception | RuntimeException exc),我相信是由于继承而导致的语法错误?

解决方案

该问题的前提是有缺陷的,因为捕捉异常 catch RuntimeException 。演示代码:

  public class Test {
public static void main(String [] args){
尝试{
抛出新的RuntimeException(Bang);
} catch(Exception e){
System.out.println(I catch:+ e);
}
}
}

输出:

 我抓到:java.lang.RuntimeException:Bang 

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




  • 回调是null

  • 任何修改回调,而循环正在执行(如果它是一个集合而不是数组)



也许这是你看到的?


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

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

but

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

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.

This is pretty relevant to me since I have a situation where RuntimeExceptions 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);
   }
}

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.

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?

解决方案

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

Output:

I caught: java.lang.RuntimeException: Bang

Your loop will have problems if:

  • callbacks is null
  • anything modifies callbacks while the loop is executing (if it were a collection rather than an array)

Perhaps that's what you're seeing?

这篇关于为什么不捕获异常捕获RuntimeException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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