Java中finally块后语句的编译错误

Java中finally块后语句的编译错误,java,Java,我有一个问题,我自己有一个非常直观的答案。这个问题与Java中的try-catch-finally块有关。就在前几天,我在尝试一些东西时,遇到了这个编译错误。我也经历了这些,回答了我的许多疑问(特别是评论2) 我在这里寻找的是为什么我在注释从#11到#13的行时会看到编译错误(基本上我是在注释内部catch块)。当取消注释相同的行时,编译和运行时执行运行良好。如果我能从OOPS环境中得到更多的答案,这将增加我的知识&这将是非常有帮助的 提前感谢 public class TryCatchFina

我有一个问题,我自己有一个非常直观的答案。这个问题与Java中的try-catch-finally块有关。就在前几天,我在尝试一些东西时,遇到了这个编译错误。我也经历了这些,回答了我的许多疑问(特别是评论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
}}

内部try的最后一行抛出异常,这意味着只执行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
}}

将起作用。

正如其他人所观察到的,您在第10行抛出一个
异常。那之后会发生什么

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

不带内挡块 当内部
catch
块被删除时,您将在第10行抛出
异常
,然后直接转到第14行的
finally
块。但该异常尚未捕获,因此仍处于活动状态。当您退出内部
finally
时,没有其他
catch
块,因此您的方法将立即返回并将异常传递给调用链。(在本例中,这并不遥远,因为它是
main
方法。)关键是第17行上的
println
由于异常永远无法执行

Java编译器非常聪明,能够发现在执行第17行的程序中没有可能的执行路径,因此它会给您一个编译错误(第22行也是如此)

诸如此类
如果在
finally
块之后不需要执行任何操作,则不需要内部捕捉。如果您想在
finally
块之后保持该方法的活动状态并执行某些操作,则需要在第10行保留内部
catch

,您正在抛出一个需要处理的异常。 由于您没有处理该异常,jvm无法编译第10行之后的代码。这就是jvm给出错误“无法访问的代码”的原因。 因此,解决方案是通过捕获异常来处理该异常
或者不要在第10行抛出异常。

您看到了什么错误?因为您的代码确实抛出了新异常(),你永远也听不到,编译器只是告诉你不可能执行这两条语句。无法访问的代码。实际的问题是:当我取消注释第11行到第13行时,我可以成功地执行,但是当我注释这些行(即内部catch块)时,为什么它失败了??!!!!因为您在第10行抛出异常,所以抛出新异常();代码的其余部分变得不可访问,编译器显示Samebe的错误,因为您抛出了一个未处理的异常,这意味着它最终仍将运行,但随后将异常传播到调用方法