• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception handling unreachable code after try-catch block

 
Murat Kutluer
Greenhorn
Posts: 20
IntelliJ IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Class A2 gives compile error beacuse last line is unreachable. Class A has an unreachle line too at the same position, but it compiles. Do you have any idea?



Thanks in advance,
Murat
 
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 Murat Kutluer,

First of all, a warm welcome to CodeRanch!

Murat Kutluer wrote:Class A2 gives compile error beacuse last line is unreachable. Class A has an unreachle line too at the same position, but it compiles. Do you have any idea?

Yes, I definitely have an idea If you can spot the difference between class A and class A2, you'll know the reason why class A2 gives a compiler error. Let's see if you can spot the reason why yourself.

Hope it helps!
Kind regards,
Roel
 
Murat Kutluer
Greenhorn
Posts: 20
IntelliJ IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel,

Thank you so much.

I actually generated the code to post here. I know the difference but I dont know why it does like that. Both have a statement after try-catch block. The difference is that in class A2 last catch block has a throw new exception statement.

Murat
 
Henry Wong
author
Marshal
Pie
Posts: 22103
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Murat Kutluer wrote:
I actually generated the code to post here. I know the difference but I dont know why it does like that. Both have a statement after try-catch block. The difference is that in class A2 last catch block has a throw new exception statement.


Simply, in order for the compiler to determine that the code is unreachable, it must check every possible path. In your first example, the compiler sees a possible path. And in the second case, it doesn't. Based on the difference, can you speculate why?

Henry
 
Murat Kutluer
Greenhorn
Posts: 20
IntelliJ IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,

I tried lots of variation and come to a conclusion that if the all catch blocks that coming after the working catch  block (in this case NullPointerException) throw an exception, this causes to compile error. If one them do not throw an exception, it works. This seems reasonable.



Murat
 
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
Murat Kutluer wrote:I tried lots of variation and come to a conclusion that if the all catch blocks that coming after the working catch  block (in this case NullPointerException) throw an exception, this causes to compile error. If one them do not throw an exception, it works. This seems reasonable.

That's indeed the reason why. If each catch block throws a (new) exception, the statement after the try-catch-finally block can never be reached. After executing the throw new XxxException(); statement (in any catch block), the finally block will be executed as finally is executed (almost) always and after completion of the finally block, the execution of this method is terminated (and thus a statement after the try-catch-finally block can never be reached).

Let's have a small pop quiz. What do you think will happen if you try to compile class A3?

Hope it helps!
Kind regards,
Roel
 
Murat Kutluer
Greenhorn
Posts: 20
IntelliJ IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This one compiles too. This is actually a bit weird. This one and the first sample class A both have a unreachable statement after try-catch block, but compiler ignores or can not get the situation.

Murat
 
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
Murat Kutluer wrote:This one compiles too. This is actually a bit weird. This one and the first sample class A both have a unreachable statement after try-catch block, but compiler ignores or can not get the situation.

You are spot-on! Because of the if statement on line8, the compiler thinks that the throw new NullPointerException(); statement will be executed conditionally and therefore in some cases this statement will not be executed and thus the statement after the try-catch-finally block will be reachable (although the if block will always be executed).

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 these topics with excellent explanations and illustrative code snippets:
  • 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
  • good examples of unreachable code?

  • I think you know what to do this evening

    Hope it helps!
    Kind regards,
    Roel
     
    Mike Simmons
    Ranch Hand
    Posts: 3090
    14
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    [Posted before seeing Roel's reply; consider this a separate response to Murat - Mike]

    The notion of what is "reachable" can be somewhat counterintuitive.  The compiler is required to follow certain rules about what it considered reachable, and what isn't.  These rules don't always match up with what is really reachable, or not.   But the compiler is required to follow them consistently, because Sun and Oracle don't want code to compile successfully on some compilers but not others.

    In your original class A, the println is considered reachable because of the catch NumberFormatException, which, if an exception were caught there, would cause program execution to proceed on to the println statement.  In reality, there's no way that the code shown would ever throw a NumberFormatException, so this isn't really reachable.  However the compiler is required to assume that an unchecked exception could possibly be thrown from any code, anywhere, with no warning.  Since NumberFormatException is unchecked, the catch clause is considered reachable, even if there's no way it could really happen.

    In the case of if (true) example given by Roel, there's another special rule on reachability, found in the Java Language Specification, with an explanation.  Can you find the relevant rule?
     
    Murat Kutluer
    Greenhorn
    Posts: 20
    IntelliJ IDE Java PHP
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you Roel and Mike, I really appreciate your response.

    I'm studying those links. There are so many interesting cases I never aware of.

    Murat
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic