简体   繁体   English

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

[英]Compilation errors for statement after finally block in Java

I have a question for which I myself have a very intuitive answer.我有一个问题,我自己对此有一个非常直观的答案。 This question relates to the try-catch-finally blocks in Java.这个问题与 Java 中的 try-catch-finally 块有关。 Well just the other day I was trying out something and I bumped into this compilation error.好吧,就在前几天,我正在尝试一些东西,但遇到了这个编译错误。 I have also gone through Do you really need the 'finally' block which answers lot of my doubts(especially comment #2).我也经历过你真的需要“最终”块来回答我的很多疑问(尤其是评论#2)。

What I am looking for here is why do I see a compilation error when I comment lines from #11 to #13(basically I am commenting the inner catch block).我在这里寻找的是为什么当我评论从 #11 到 #13 的行时会看到编译错误(基本上我是在评论内部 catch 块)。 And the compilation and run-time execution runs fine when uncomment the same lines.当取消注释相同的行时,编译和运行时执行运行良好。 If I could get an answer more from a OOPS context, it would increase my knowledge & will be of great help.如果我能从 OOPS 上下文中获得更多答案,那将增加我的知识,并将有很大帮助。

Thanks in advance.提前致谢。 !! !!

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

As others have observed, you throw an Exception on line 10. What happens after that?正如其他人所观察到的,您在第 10 行抛出了一个Exception 。之后会发生什么?

With the inner catch block带内catch块

When the inner catch block on lines 11-13 is uncommented, you'll throw the Exception on line 10 and immediately catch it.当第 11-13 行的内部catch块被取消注释时,您将在第 10 行抛出Exception并立即捕获它。 You'll print out "Inner catch", because that's what's in the catch block, then "Inner finally".您将打印出“Inner catch”,因为那是catch块中的内容,然后是“Inner finally”。 The exception is no longer active since you caught it, so you proceed to line 17 and beyond.异常在您捕获后不再处于活动状态,因此您继续执行第 17 行及以后的操作。

Without the inner catch block没有内部 catch 块

When the inner catch block is removed, you'll throw the Exception on line 10 and go straight to the finally block on line 14. But the exception hasn't been caught, so it's still active.当内部catch块被移除时,您将在第 10 行抛出Exception并直接进入第 14 行的finally块。但该异​​常尚未被捕获,因此它仍然处于活动状态。 When you exit the inner finally , there is no other catch block, so your method will return immediately and pass the exception up the call chain.当您退出内部finally ,没有其他catch块,因此您的方法将立即返回并将异常向上传递到调用链。 (That's not very far in this case, since it's the main method.) The point is that the println on line 17 can never execute because of the exception. (在这种情况下,这不是很远,因为它是main方法。)重点是第 17 行的println由于异常而永远无法执行

The Java compiler is smart enough to figure out that there is no possible execution path through your program where line 17 gets executed, so so it gives you a compilation error for it (and for line 22, too). Java 编译器足够聪明,可以确定在程序中没有可能的执行路径来执行第 17 行,因此它会给您一个编译错误(以及第 22 行)。

And so所以

You don't need the inner catch if you don't need to do anything after your finally blocks.如果您在finally块之后不需要做任何事情,您就不需要内部捕获。 If you want to keep the method alive and execute something after your finally blocks, you need to keep the inner catch .如果您想让方法保持活动状态并在finally块之后执行某些操作,则需要保留内部catch

The last line of your inner try throws an exception, meaning only the finally blocks will be executed.内部 try 的最后一行抛出异常,这意味着只会执行 finally 块。

add another catch block, or remove that exception throw, and your problem is solved.添加另一个 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
}}

or this或这个

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

will work.将工作。

At 10th line , You are throwing an exception which needs to be handled.在第 10 行,您正在抛出一个需要处理的异常。 Since you are not handling that exception , jvm is unable to compile the code after 10th line .由于您没有处理该异常,jvm 无法编译第 10 行之后的代码。 That,s why jvm is giving the error "unreachable code" .这就是 jvm 给出错误“unreachable code”的原因。 So the solution is to either handle that exception by catching that exception or dont throw exception at 10th line.因此,解决方案是通过捕获该异常来处理该异常,或者不要在第 10 行抛出异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM