• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why this code does not throw unreachable code

 
smily sharma
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  List s1 = new ArrayList( );         try{             while(true){                 s1.add("sdfa");             }         }catch(RuntimeException e){             e.printStackTrace();         }         System.out.println(s1.size());

As there is no break and it will not go out of while unless memory is exhausted even then the last line system out will never be printed.
 
Ahmed Bin S
Ranch Hand
Posts: 378
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JVM (sorry, I of course mean compiler!) doesn't check what you are doing in the while loop. You have the loop in a try and you are catching RuntimeException, and so the compiler thinks that maybe the code inside the while loop will throw a RuntimeException, which you will catch and handle, and therefore, as far as the compiler is concerned, the println statement will be reached.

Of course, we know it won't because we can see from your code that there will be no RuntimeException thrown inside the while loop, and instead you will eventually reach an OutOfMemoryException, but the compiler doesn't do this - imagine if you had a thousand lines of code in your while loop, it is simply not possible for the compiler to work out whether a RuntimeException will be thrown or not.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

First of all, a warm welcome to CodeRanch!

smiles sharma wrote:As there is no break and it will not go out of while unless memory is exhausted even then the last line system out will never be printed.

Let's first format your code and use some code-tags so the code snippet looks very nice and is easier to read
That looks so much better!

Here is a very, very, very important rule: the compiler doesn't execute any code at all! So the compiler only knows the infinite while loop might be interrupted by a runtime exception which is handled by the catch block and therefore the println statement is reachable. And even without an empty loop block, the code will compile without compiler errors (as illustrated in this code snippet)Without the try block, the code will fail to compile due to "unreachable code". Again illustrated in the following code snippetAnd if you would have rethrown the runtime exception in the catch block, the code will fail to compile too (and again due to "unreachable code"). And here is again the illustrative code snippet

Hope it helps!
Kind regards,
Roel

PS. Always use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers. Jeanne already added the code tags for you. See how much easier the code is to read?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahmed Bin S wrote:The JVM doesn't check what you are doing in the while loop. You have the loop in a try and you are catching RuntimeException, and so the JVM thinks that maybe the code inside the while loop will throw a RuntimeException, which you will catch and handle, and therefore, as far as the JVM is concerned, the println statement will be reached.

Although your explanation is correct, I have to make one small (but important) remark. It's the task of the compiler to verify if your code is valid and doesn't contain any violations of all rules (e.g. don't use keyword as identifier, don't cast incompatible types, and so on). Once the code is successfully compiled, the JVM will execute your code. But the JVM is not involved when compiling your code.
 
Ahmed Bin S
Ranch Hand
Posts: 378
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Ahmed Bin S wrote:The JVM doesn't check what you are doing in the while loop. You have the loop in a try and you are catching RuntimeException, and so the JVM thinks that maybe the code inside the while loop will throw a RuntimeException, which you will catch and handle, and therefore, as far as the JVM is concerned, the println statement will be reached.

Although your explanation is correct, I have to make one small (but important) remark. It's the task of the compiler to verify if your code is valid and doesn't contain any violations of all rules (e.g. don't use keyword as identifier, don't cast incompatible types, and so on). Once the code is successfully compiled, the JVM will execute your code. But the JVM is not involved when compiling your code.



Yes, I already edited my post 57 minutes ago
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Luckily for you, the "unreachable code" topic is one of the more popular ones in this forum. So using the search function you'll find plenty of topics about "unreachable code". Here is a list with a few of them:
  • good examples of unreachable code?
  • return value for method.
  • Confused point about infinite loop
  • continue and break in a loop would generate compilation error?
  • Which of these is unreachable code?
  • Unreachable Code
  • Which is the first line to cause error?
  • Maybe "unreachable code" should be "dead code" in page 70, (Java OCA 8 Programmer I Study Guide)
  • Throwing a second exception question
  • System.exit() and unreachable code

  • I think you know what to do this evening and weekend

    Hope it helps!
    Kind regards,
    Roel
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ahmed Bin S wrote:Yes, I already edited my post 57 minutes ago

    Really great that you noted your own mistake and have corrected it Immediately after making my first post in this topic, I quoted your (at that time) incorrect statement and started typing a reply. Then I had to do a few other things before I could finish and submit my reply. So I had completely no idea you had already fixed your reply. Sorry about that!
     
    Ahmed Bin S
    Ranch Hand
    Posts: 378
    8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Roel De Nijs wrote:
    Ahmed Bin S wrote:Yes, I already edited my post 57 minutes ago

    Really great that you noted your own mistake and have corrected it Immediately after making my first post in this topic, I quoted your (at that time) incorrect statement and started typing a reply. Then I had to do a few other things before I could finish and submit my reply. So I had completely no idea you had already fixed your reply. Sorry about that!


    Yes, I realised that is what must have happened
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic