Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

jdk compiler unreachable code

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

I have a question regarding the functionality of the java compiler.
If I add in my code something like:

throw new NullPointerException();
System.out.println();

At the line with System.out.println(); java compiler fails to compile, because of "Unreachable code".

If I add something like:

System.exit(0);
System.out.println();

The compiler compiles the code successful.

Why the difference between the two piece of code?

Thanks
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,

Welcome to CodeRanch!

The reason for first code is:
Whenever you throw an exception (without any condition) - obviously, the next line from same scope is not executed. Thus, here, compiler gives unreachable code error (because there is guarantee that next line will never be reached).

For second code - from compiler's POV, it is simply a method call. It seems that compiler is not intelligent enough to 'decode' the meaning of System.exit at compile time (what if you use some custom classloader which loads your own 'System' class - which is having an 'exit' method - but it does not actually exits the JVM?). Thus, compiler will not give unreachable code error.

I hope this helps.
 
Rancher
Posts: 43016
76
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler knows what throwing an exception means, but it has no idea what side effects calling System.exit has. As far as the compiler is concerned, it's a method call like any other, and code execution will resume at the next statement after it.

Edit: ... what he said :-)
 
Peter Zsolt
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the answers. Good reasons. It helps me to understand in more detail the compiler.
 
What a stench! Central nervous system shutting down. Save yourself tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic