• 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Question for Kathy on Unreachable code

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kathy, I have just completed ch5 of ur book.
I must say Bert & u have written a great book but still i think u havn't covered some key topics such as :
1. Why certain code is considered unreachable by the compiler ?
2. How does the compiler deal with Exceptions that are not thrown by a try block.
3. Why static methods can't be overriden i.e u havn't covered Hiding
Still a great book.
Well now lets come to the point. i have attempted many mock questions so far and m constantly scoring around 90-100% on topics I have read so far.
Most of the Questions I get wrong especially from Dan's include the topics listed above.
For unreachable code I have tried JLS 14.20 but it doesn't make a good reading.
It wud be very kind of u if u cud explain me how the compiler decides that certain code is unreachable
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mohit,
Thank you for using my exams.
You are correct, JSL section 14.20 is the best place to go for information on unreachable statements. Unfortunately, you are also correct in that it isn't one of the most interesting sections of the book.
Your question is very general and a response would require a restatement of section 14.20. Could we start with a more specific question?
 
Mohit Goyal
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class A {

public static void main(String[] args){
while(false)
System.out.print(4);
}
}
This gives compiler error unreachable code
But the following doesn't give any compiler error.
public class A {

public static void main(String[] args){
boolean b=false;
while(b)
System.out.print(4);
}
}
Why is this so
 
stable boy
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your example "false" is a constant expression and can never change.
According to the JLS: "The while statement is reachable and the condition expression is not a constant expression with value true. "
In your second example the value of "b" can change.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If you declare 'b' as a final variable ( compile time constant), you will get the same error.
Regards,
Hari
 
Mohit Goyal
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is a ques from Dan's Exam on Exception Handling
class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Purple {
public static void main(String args[]) {
int a,b,c,d,f,g,x;
a = b = c = d = f = g = 0;
x = 1;
try {
throw new Level1Exception();//1
try {
switch (x) {
case 1: throw new Level1Exception();
case 2: throw new Level2Exception();
case 3: throw new Level3Exception();
} a++; }
catch (Level2Exception e) {b++;}
finally {c++;}
}
catch (Level1Exception e) { d++;}
catch (Exception e) {f++;}
finally {g++;}
System.out.print(a+","+b+","+c+","+d+","+f+","+g);
}}
What is the result of attempting to compile and run the program?
a. Prints: 1,1,1,0,0,1
b. Prints: 0,1,1,0,0,1
c. Prints: 0,1,0,0,0,0
d. Prints: 0,1,0,0,0,1
e. Prints: 0,0,1,0,0,1
f. Compile-time error
g. Run-time error
h. None of the above
the ans is f.Compile time error (Unreachable code)
how this is decided and if we wud have invoked a method at line labelled 1 that throws the same exception wud we get the same error
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mohit,
The throw statement always causes control to pass out of the try block, so the switch statement can not ever be reached. A method that declares an exception in the throws clause might throw an exception or it might run to completion without error. Since we don't know what the method might do at run-time, we can not say that subsequent statements are unreachable.
 
Let nothing stop you! Not even this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic