Is there a way to ignore the 'Unreachable statement' error?

后端 未结 5 517
耶瑟儿~
耶瑟儿~ 2020-12-13 17:41

Is it possible to somehow ignore this error? I find it much easier to just put return in front of the code I don\'t want to run than to comment it (when the com

相关标签:
5条回答
  • 2020-12-13 17:44

    you have to fix that unreachable code.

    public void display(){
      return; //move the return statement to appropriate place
      int i;
    }
    

    compiler will not compile your source code. you have to take care of your source code that every line is reachable to compiler.

    0 讨论(0)
  • 2020-12-13 17:54
    33. if (1==1) return;
    34. System.out.println("Hello world!");
    

    It works in other languages too. But ByteCode without row 34.

    0 讨论(0)
  • 2020-12-13 17:54

    It isn't possible to ignore this error since it is an error according to the Java Language Specification.

    You might also want to look at this post: Unreachable code error vs. dead code warning in Java under Eclipse?

    0 讨论(0)
  • 2020-12-13 18:08

    If you want disable/enable certain piece of code many times trick from old C may help you:

    some_code();
    more_code();
    // */
    
    /*
    some_code();
    more_code();
    // */
    

    Now you need only to write /* at the beginning

    0 讨论(0)
  • 2020-12-13 18:09

    No. It's a compile time error. So you must get rid of it before running your class.

    What I usually do is put a fake if statement in front of it. Something like:

    if(true)
        return;
    // unwanted code follows. no errors.
    i++;
    j++;
    

    With this code, you will not get a Unreachable statement error. And you will get what you want.

    0 讨论(0)
提交回复
热议问题