区块捕获中的丢失异常 [英] loss exception in block catch

查看:76
本文介绍了区块捕获中的丢失异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行以下代码:

public class User {

    public static void main(String args[]) {
        int array[] = new int[10];
        int i = 1;
        try {
            System.out.println("try: " + i++);
            System.out.println(array[10]);
            System.out.println("try");
        } catch (Exception e) {
            System.out.println("catch: " + i++);
            System.out.println(array[10]);
            System.out.println("catch");
        } finally {
            System.out.println("finally: " + i++);
            Object o = null;
            o.hashCode();
            System.out.println("finally");
        }

    }
}




结果:
尝试:1
catch:2

最后:3
线程
中的异常 main java.lang.NullPointerException
在user.main(User.java:17)

Result:
try: 1
catch: 2
finally: 3
Exception in thread "main" java.lang.NullPointerException at user.main(User.java:17)

块捕获中-ArrayIndexOutOfBoundsException,但是我们丢失了此异常,为什么?

in block catch - ArrayIndexOutOfBoundsException, but we loss this Exception, why?

推荐答案

来自JLS



您可以在JLS中阅读此内容,块和语句,第 14.19节 .2最终执行try-catch。我引用

From the JLS

You can read about this in the JLS, Blocks and Statements, section "14.19.2 Execution of try-catch-finally". And I quote,


如果try块的执行由于其他任何原因R突然完成,那么将执行finally块。然后可以选择:

  • 如果finally块正常完成,则try语句由于原因R突然完成。

  • 如果最后,块由于原因S突然完成,然后try语句由于原因S突然完成(并且原因R被丢弃)。示例...

因此,以下内容(实际上是从发问者的代码中精炼出来的)用NPE完成,而不是抛出 ExceptionTest

Therefore, the following (which is really condensed from the questioner's code) completes with an NPE, not the ExceptionTest thrown.

class Phinally
{
  static class ExceptionTest extends Exception
  { public ExceptionTest(String message) { super(message); }  }

  public static void main(String[] args) throws ExceptionTest
  {
    try {
      System.out.println("Foo.");
      throw new ExceptionTest("throw from try"); 
    } finally {
      throw new NullPointerException("throw from finally");
    }    
  }
}



大约<$ c的边栏$ c>尝试,带有资源/ ARM块



在某些常见情况下,尤其是在管理资源时需要对此进行推理,并且需要嵌套的尝试 / 捕获 / 最终块,并嵌套在<$ c $内c> finally 块,是项目COIN中尝试使用资源功能的原因之一(将很快集成到Java中),您可以阅读更多有关此处的信息

A Sidebar about try with resources/ARM blocks

Difficulties reasoning about this in some common cases specifically with managing resources, and requiring nested try/catch/finally blocks, and nested inside finally blocks, are part of the reason for the "try with resource" feature in project COIN (to be integrated into Java "fairly soon"), about which you can read more about here.

这是其中之一有很多充分的理由可以花时间运行静态分析器,例如 PMD ,它可以找到并抱怨这种类型的混乱-尽管在y时可能无法解决问题我们的代码不确定。

This is one of many good reasons to invest the time in running a static analyzer like PMD, which finds and complains about this type of confusion -- though it might not catch the case in your code, I'm not sure.

关注@stacktrace的评论:我通过PMD和查找错误,尝试以下两种操作:

Follow up on comment from @stacktrace: I ran the relevant code through PMD and FindBugs, trying both of the following:

finally { throw NullPointerException("Foo"); }

finally { Object o = null; System.out.println(o.toString()); }

对于前者,PMD注意到并抱怨最终子句。 FindBugs一点也不抱怨。对于后者,PMD抱怨了几件事,但没有任何关联( LocalVariableCouldBeFinal, StringToString和 UselessOperationOnImmutable)。但是,FindBugs注意到并抱怨取消空引用。故事的道德启示?同时运行PMD和FindBugs!

For the former, PMD noticed and complained about an exception being thrown from a finally clause. FindBugs doesn't complain at all. For the latter, PMD complained about several things but nothing related ("LocalVariableCouldBeFinal", "StringToString", and "UselessOperationOnImmutable"). However, FindBugs noticed and complained about a null dereference. Moral of the story? Run both PMD and FindBugs!

与SO相关:在catch / finally中抛出的吞咽异常我可以避免这种麻烦的try / catch / finally ...

这篇关于区块捕获中的丢失异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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