• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

System.exit() and unreachable code

 
Mark Justison
Ranch Hand
Posts: 31
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

So I discovered this by accident and was a bit surprised by the result.
I compiled ran some code that I forgot included 'System.exit(0);' with some code beneath it.
Why does the compiler allow this when it is basically code that can never be reached?

Thanks!
-Mark
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3817
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Justison wrote:Hi,

So I discovered this by accident and was a bit surprised by the result.
I compiled ran some code that I forgot included 'System.exit(0);' with some code beneath it.
Why does the compiler allow this when it is basically code that can never be reached?

Thanks!
-Mark

Compiler doesn't execute any code. It cannot possibly know that this method will cause the JVM to shut down.
 
Mark Justison
Ranch Hand
Posts: 31
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to pass an argument to exit() that won't cause the system to exit, then?
Or simply the nature of the compiler to not know what the method does?
 
Henry Wong
author
Marshal
Pie
Posts: 22092
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It is possible to interpose or replace jar files ... so, the answer is yes to both options. However, the more correct answer is the latter.

Henry
 
Tim Holloway
Saloon Keeper
Posts: 18359
56
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.exit() is simply a method. There are no "magic methods" in Java that the compiler treats specially. Thus, the compiler cannot and does not assume that any particular method of any particular class will not return.

Failing such knowledge, the compiler cannot flag subsequent code as unreachable.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Justison wrote:Why does the compiler allow this when it is basically code that can never be reached?

Because the compiler does not execute any code at all! That's a very, very, very important rule.

Mark Justison wrote:Is it possible to pass an argument to exit() that won't cause the system to exit, then?
Or simply the nature of the compiler to not know what the method does?

No, because the status parameter is nothing more but a status code about the termination of the application (normal, abnormal,..).

But that doesn't matter. The compiler does not execute any code, because that's the chicken or the egg dilemma. In order to run the code, you need .class files. But in order to get those .class files, the code must compile successfully. But in order to know if the code compiles successfully you need to run the code...

And in this code it would be an easy spot for the compilerBut what about this code. It's already a lot harder for the compiler and requires executing our custom exit() method

Hope it helps!
Kind regards,
Roel
 
Tim Holloway
Saloon Keeper
Posts: 18359
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's possible that there may be some confusion because some C compilers did have "magic methods". For example, in my old Amiga C compiler, there was a macro that redefined "printf" as something like "internal_printf" and when "internal_printf" was seen by the compiler, it knew to scan the source code very carefully. That allowed stuff like compile-time type checking of format strings against their arguments, making sure that the argument count was equal to the format count and smoothing out various warts in what is at heart a very irregular function. Another alternative (although this was never done) would be to syntax-check SQL command strings when something like a "sqlConnection.createStatement" method was invoked.

But I repeat. Java has no magic methods. What you see is what you get.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic