• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Unreachable Code

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does this code compile?
finally block is unreachable code, right?

 
Marshal
Posts: 6206
501
IntelliJ IDE Python TypeScript Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, and welcome to the Ranch!

Do you think the Java compiler considers unreachable code an error?
 
Nayan Gupta
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe so
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to run the code in finally clause, use "return" or throw some exception instead of exit().
 
Nayan Gupta
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree Petr, the intention is not to run the code but to understand about unreachable code for exam preparation (OCP Java21)
 
Petr Šlechta
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Java cannot declare a method as "noreturn". At least System.exit(int) is not declared so (see System.exit(int)). So the compiler does not know that System.exit() never returns.
 
Greenhorn
Posts: 26
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look Oracle system class. Method exit() terminates JVM. The compiler doesn’t treat the finally block as unreachable, because it cannot predict runtime behavior. Compilation only checks syntax, not what actually executes.
 
Rancher
Posts: 5250
85
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JLS 14.22 Unreachable Statements gives precise rules for how Java determines whether code is considered "unreachable" in a way that causes a compiler error.  Unfortunately, it's not easy reading.  But the most relevant part here is that when determining if code is unreachable, if the compiler sees any method call, it will never use any knowledge of what that method does internally, in the body of the method declaration.  In fact I think the only thing that it will look at from a method declaration is the throws list - it can use knowledge of checked exceptions thrown by methods to determine whether a given catch block is reachable or not.  But in general, for reachability, the compiler does not know or care what a method call does.  It essentially assumes that it could complete normally, or that it could throw an unchecked exception, or a checked exception if it's declared to be able to do so.  That's all that it knows about any method call, when determining if code is reachable.
 
Marshal
Posts: 81613
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JLS even permits if (false) foo(); to be regarded as reachable; that is exp;ained in the section MS linked.
 
Campbell Ritchie
Marshal
Posts: 81613
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:. . . the compiler . . . can use knowledge of checked exceptions . . . to determine whether a given catch block is reachable or not. . . .

The following little example shows this, compiled on JShell:-

jshell> void foo()
  ...> {
  ...>     try
  ...>     {
  ...>         System.out.println("CodeRanch is brilliant.");
  ...>     }
  ...>     catch (java.io.IOException ex)
  ...>     {
  ...>         ex.printStackTrace();
  ...>     }
  ...> }
|  Error:
|  exception java.io.IOException is never thrown in body of corresponding try statement
|      catch (java.io.IOException ex)
|      ^-----------------------------...

jshell>

I think you can get catch (Exception ex) ... to compile, because Exception has unchecked subtypes. Of course, I avoid catch (Exception ex) ... and throwing and catching the same exception in the same method as far as possible.
 
You get good luck from rubbing the belly of a tiny ad:
Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders
https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing
reply
    Bookmark Topic Watch Topic
  • New Topic