• 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Unreachable statement

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
Please see the code below.

import java.io.IOException;
public class Str {
public static void main(String[] args)throws Exception {
try {
m1();
System.out.println("After m1"); // Line 2
}
finally {
// throw new IOException(); //Line 1
System.out.println("Inside finally");
}
}
public static void m1() throws Exception {
throw new Exception();
}
}

This code compiles and runs fine (fine means throws exception properly since I don't have a catch block). But if I uncomment Line 1, then compiler gives error:
Statement not reached: System.out.println("Inside finally");
So my question is, after commenting Line1 again, in try block I am calling a method, m1, which is throwing an exception, So Line 2 is definitely not going to get executed at any cost. Then why doesn't compiler give error that Line 2 is unreachable. Why the rules are different for try and finally blocks?
I am definitely missing some fundamentals here, please let me know what the rules are.
Thanks
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Seema,
Here is my shot at explaining this. I think this is related to what kind of checks can be done by a compiler and what it cannot do. It is obvious to a programmer that method m1 called in the try block throws an exception, so the print statement after the call to m1 is unreachable for us. Does the compiler have that kind of intelligence to see a call to m1 and then check the code in m1 to arrive at the same conclusion ? That would be way too complicated to ensure.
Yet, it can perform simpler and more obvious checks. So if you replace the call to m1 with a statement that throws an exception outright, the compiler complains. Try this-

So if you notice, the behaviour in try and finally blocks or anywhere else for that matter is the same when you throw an exception outright. Try calling m1() in the finally block and it will not complain again.
Do correct me if I am wrong.
Cheers
Sajida
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, let me have a shot.
There's 2 techinal terms should be unterstood before going deep into the unreachable issues. they are reachable and can complete nomally, you could refer to them in chapter 14.20 of the jls, here are 2 segments from the same chapter concerning the current question.


An empty block that is not a switch block can complete normally iff it is reachable. A nonempty block that is not a switch block can complete normally iff the last statement in it can complete normally. The first statement in a nonempty block that is not a switch block is reachable iff the block is reachable. Every other statement S in a nonempty block that is not a switch block is reachable iff the statement preceding S can complete normally.
An expression statement can complete normally iff it is reachable.


Since method invocations are regarded as expression so they always can complete normally provided they're reachable. Note, the code in the method are not taken into account.
The rest reasoning go without saying.
Hope it help
[This message has been edited by James Du (edited May 09, 2001).]
 
Seema Das
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to Sajid and James! but I think I am still not clear about the concept.
[This message has been edited by Seema Das (edited May 10, 2001).]
 
sajida kal
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seema,
If you tell me what portion of the explanation you did not
understand, I can try and help you.
btw, its Sajida not Sajid. The missing 'a' makes a difference between feminine and masculine versions of the name!
Cheers
 
Seema Das
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there is a lot of difference between Sajid and Sajida. I apologize for messing up your name Sajida!
well, could you guys let me know where can I get information on this topic? I have read Khalid Mughal but I didn't find anything there. I missed one more question on this in JQ+ today, so I definitely have to read about it.
Any input will be appreciated.
Thanks
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seema said, "but I think I am still not clear about the concept."
In this scenario, you ALWAYS get to the System.out.println statement:
finally
{
System.out.println("Inside finally");
}


But here, if you throw the exception, you won't reach the println - and that gives you the compiler error:
finally
{
throw new IOException(); //IF I EXIT HERE, I WON'T REACH THE NEXT STATEMENT
System.out.println("Inside finally"); //I MAY NEVER GET HERE!
}

It's not really an issue of 'finally' executing differently. For example, I bet you could get the same type of compiler error if you used break statements, or had something in some if-else logic that kept you from reaching a statement.
For example, this would probably give you the same unreachable statement error:
switch (something)
case 1:
break;
++x; //Will never get here. Should give an error.
case 2:
break;
Susan

[This message has been edited by Susan Delph (edited May 10, 2001).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic