Unreachable Code
posted 3 months ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Why does this code compile?
finally block is unreachable code, right?
finally block is unreachable code, right?
posted 3 months ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hello, and welcome to the Ranch!
Do you think the Java compiler considers unreachable code an error?
Do you think the Java compiler considers unreachable code an error?
Tim Driven Development | Test until the fear goes away
Nayan Gupta
Greenhorn
Posts: 4
posted 3 months ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
If you want to run the code in finally clause, use "return" or throw some exception instead of exit().
Nayan Gupta
Greenhorn
Posts: 4
posted 3 months ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
posted 3 months ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
posted 3 months ago
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
posted 3 months ago
-
2 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
posted 3 months ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
posted 3 months ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
The following little example shows this, compiled on JShell:-Mike Simmons wrote:. . . the compiler . . . can use knowledge of checked exceptions . . . to determine whether a given catch block is reachable or not. . . .
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.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>
| 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 |








