【问题标题】:Compilation errors for statement after finally block in JavaJava中finally块后语句的编译错误
【发布时间】:2015-10-29 11:54:32
【问题描述】:

我有一个非常直观的答案。这个问题与 Java 中的 try-catch-finally 块有关。好吧,就在前几天我在尝试一些东西时遇到了这个编译错误。我也经历了Do you really need the 'finally' block,它回答了我的很多疑问(尤其是评论#2)。

我在这里寻找的是为什么我在注释 #11 到 #13 的行时会看到编译错误(基本上我是在注释内部 catch 块)。当取消注释相同的行时,编译和运行时执行运行良好。如果我能从 OOPS 上下文中得到更多答案,那将增加我的知识并且会有很大的帮助。

提前致谢。 !!

public class TryCatchFinally {
  public static void main(String[] args) throws Exception {
    try {
        System.out.println("Try...");
        throw new Exception();          
    } catch (Exception e) {
        System.out.println("Catch...");
        try {
            System.out.println("Inner Try");
            throw new Exception();
        } /*catch(Exception e1) {//Line #11
            System.out.println("Inner catch");
        }*///Line #13
        finally{
            System.out.println("Inner finally");
        }
        System.out.println("Going out of catch..");//Line #17
    }
    finally{
        System.out.println("Finally");
    }
    System.out.println("Going out of try..");//Line #22
}}

【问题讨论】:

  • 你看到了什么错误?
  • 由于您的代码执行throw new Exception();,而您永远不会捕获,编译器只是告诉您不可能执行这两个语句。
  • 无法访问的代码。实际的问题是:当我取消对第 11 行到第 13 行的注释时,我可以成功执行,但是当我注释这些行(即内部 catch 块)时,为什么它会失败.. ??!!!!
  • 因为你在第 10 行抛出异常 throw new Exception();其余代码变得无法访问,并且编译器显示相同的错误
  • 因为你抛出了一个你没有处理的异常,这意味着它最终仍会运行,但随后会将异常传播给调用方法

标签: java


【解决方案1】:

正如其他人所观察到的,您在第 10 行抛出 Exception。之后会发生什么?

使用内部 catch 块

当第 11-13 行的内部 catch 块未注释时,您将在第 10 行抛出 Exception 并立即捕获它。您将打印出“Inner catch”,因为这是catch 块中的内容,然后是“Inner finally”。该异常在您捕获后不再处于活动状态,因此您继续执行第 17 行及以后的操作。

没有内部 catch 块

当内部的catch 块被移除时,你会在第10 行抛出Exception 并直接进入第14 行的finally 块。但是该异常还没有被捕获,所以它仍然处于活动状态.当您退出内部finally 时,没有其他catch 块,因此您的方法将立即返回并将异常传递到调用链。 (在这种情况下,这不是很远,因为它是 main 方法。)关键是第 17 行的 println 由于异常而永远无法执行

Java 编译器足够聪明,可以确定在您的程序中没有可能执行第 17 行的执行路径,因此它会给您一个编译错误(以及第 22 行)。

等等

如果您在 finally 块之后不需要执行任何操作,则不需要内部捕获。如果你想在你的finally 阻塞之后保持方法活着并执行一些东西,你需要保持内部的catch

【讨论】:

    【解决方案2】:

    你的内部尝试的最后一行抛出一个异常,这意味着只有 finally 块将被执行。

    添加另一个 catch 块,或者删除那个异常抛出,你的问题就解决了。

    public class TryCatchFinally {
      public static void main(String[] args) throws Exception {
        try {
            System.out.println("Try...");
            throw new Exception();          
        } catch (Exception e) {
            System.out.println("Catch...");
            try {
                System.out.println("Inner Try");
                throw new Exception();
            } catch(Exception e1) {//Line #11
                System.out.println("Inner catch");
            }//Line #13
            finally{
                System.out.println("Inner finally");
            }
            System.out.println("Going out of catch..");//Line #17
        }
        finally{
            System.out.println("Finally");
        }
        System.out.println("Going out of try..");//Line #22
    }}
    

    或者这个

    public class TryCatchFinally {
      public static void main(String[] args) throws Exception {
        try {
            System.out.println("Try...");
            throw new Exception();          
        } catch (Exception e) {
            System.out.println("Catch...");
            try {
                System.out.println("Inner Try");
               // throw new Exception();
            } /*catch(Exception e1) {//Line #11
                System.out.println("Inner catch");
            }*///Line #13
            finally{
                System.out.println("Inner finally");
            }
            System.out.println("Going out of catch..");//Line #17
        }
        finally{
            System.out.println("Finally");
        }
        System.out.println("Going out of try..");//Line #22
    }}
    

    会起作用的。

    【讨论】:

      【解决方案3】:

      在第 10 行,您抛出了一个需要处理的异常。 由于您没有处理该异常,jvm 无法在第 10 行之后编译代码。这就是为什么 jvm 给出错误 "unreachable code" 的原因。 所以解决方案是通过捕获该异常来处理该异常 或者不要在第 10 行抛出异常。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-31
        • 2013-06-04
        • 2018-03-21
        • 2019-02-18
        • 1970-01-01
        • 2014-08-20
        • 2015-04-06
        • 2022-11-30
        相关资源
        最近更新 更多