Unreachable return statement still throws error

橙三吉。 提交于 2019-12-25 02:28:23

问题


I have this very simple code snippet:

static String getInput() throws IOException{
  if(in.ready()){
      return in.readLine().trim();
  }
  System.err.println("Please provide more input in order to execute the program.");
  System.exit(0);
  return "";
}

By what I think I know, there is no possible way that the JVM will execute the return statement at the end of the code. But if I comment this line out, java will complain about a missing return statement. Why doesn't the JVM recognize that a System.exit(0) will not allow any other code to execute, but complains about unreachable statements if a return will not allow code to be executed? I think the return statement at the end is redundant and might be confusing to other devs, so why won't java let me get rid of it?


回答1:


Why doesn't the JVM recognize that a System.exit(0) will not allow any other code to execute, but complains about unreachable statements if a return will not allow code to be executed?

It's not the JVM - it's the compiler. And the compiler doesn't know what library calls will do - it only knows the language rules. (In particular, JLS section 14.21, Unreachable Statements.)

So for example:

public int foo() {
    alwaysThrow();
    // This is required.
    return 10;
}

private static void alwaysThrow() {
    throw new RuntimeException();
}

vs

public int foo() {
    throw new RuntimeException();
    // Error: unreachable statement
    return 10;
}

That simple inlining changes the meaning of the code as far as the compiler's concerned.

This could be "fixed" by having a return type of "never" - to indicate "this method never returns normally - it either hangs or throws an exception" but that's simply not part of the language (and would have its own complications). If you're interested, Eric Lippert has a couple of blog posts on this topic with regard to C# (which is in a similar position): part one, part two.



来源:https://stackoverflow.com/questions/21336303/unreachable-return-statement-still-throws-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!