This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Un reachable statement (Sorry for posting the same topic second time...)

 
SaiPrasad Jukalkar
Ranch Hand
Posts: 79
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for posting the same topic second time,
first one did'nt have proper indentation in the code
quote:
--------------------------------------------------------------------------------
Originally posted by Shyamsundar Gururaj:
Well, I've come to my lab now...Here's the dirty stuff regarding the dark connection between the return statement and the unreachable statement error...
code:
public class returntest{
public static void main(String[] args){
returntest a = new returntest();
a.test();
}
void test(){
int x = 5;
try{
return;
// Specifying a return here is OK and will not cause an error in **5**
}catch(Exception e) {
System.out.println("In catch : " + x);
} System.out.println(x); //5
}
}
public class returntest{
public static void main(String[] args)
{
returntest a = new returntest();
a.test();
}
void test(){
int x = 5;
return;
// If return is not enclosed within a try catch block, 5 will be blocked.
System.out.println(x); //5
}
}
public class returntest{
public static void main(String[] args){
returntest a = new returntest();
a.test();
}
int test(){
int x = 5;
return 5;
// If the function specifies a return value and returns one such as this one does,// in the absence of a try/catch block, the compiler throws 2 errors // (1).Missing return statement??? and (2) the infamous unreachable statement. System.out.println(x); // **5** }} public class returntest{ public static void main(String[] args){ returntest a = new returntest(); a.test(); } int test(){ int x = 5; try{ return 5; }/* If the function returns a value and even if the return statement is enclosed within a try-catch block, the compiler throws an error. However, if the function does not return any value, then it is fine (but the try-catch block must remain intact). Irrespective of whether a function returns a value or not, even if the return statement is enclosed within a try block and there a finally block but no catch block, the compiler launches into hysteria. */
catch(Exception e){
System.out.println("In catch : " + x);
}
finally{
System.out.println("In Finally : " + x);
}
System.out.println(x); // 5
}
}
--------------------------------------------------------------------------------
Well...I told you this stuff would be nasty...
Cheers!
Shyam
[This message has been edited by Shyamsundar Gururaj (edited October 30, 2001).]


--------------------------------------------------------------------------------

when i compiled some of the code from above discussion, i was getting compiler error.
getting error in the following code for "Unreachable statement."
public class returntest{
public static void main(String[] args){
returntest a = new returntest();
a.test();
}
void test(){
int x = 5;
try{
return; // Specifying a return here is OK and will not cause an error in **5**
}catch(Exception e)
{
System.out.println("In catch : " + x);
}
System.out.println(x); // **5**
}
}
i apologize if you have already discussed this before.
thanks in advance.
Saiprasad.
 
David Weitzman
Ranch Hand
Posts: 1365
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the button to edit your posts.
Also (note how I edited my own post) you can use the tags to include indented code. Look at the UBB Code info page.
[This message has been edited by David Garland (edited December 17, 2001).]
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi SaiPrasad,
Now that you posted it twice what is your problem? You are showing us code that has numerous comments in it and not asking anything from us. Use smaller examples when you post. That will do 1 of 2 things:
1. You will figure out your own problem by trying to get a small test program for help, or
2. Someone will be able to help you here without psychic intervention.
Regards,
Manfred.
 
SaiPrasad Jukalkar
Ranch Hand
Posts: 79
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks david for a valuable solution.
Manfred Leonhardt,
i apologize for not properly indenting, actually i indented all the code in a java editor and pasted here, i dont no how it became unindented.
here my question is
when i compiled some of the code from http://www.javaranch.com/ubb/Forum24/HTML/012797.html
i was getting compiler error.
got error in the following code for "Unreachable statement."
public class returntest{
public static void main(String[] args){
returntest a = new returntest();
a.test();
}
void test(){
int x = 5;
try{
return;
/*Specifying a return here is OK and will not cause an error in 5*/
}catch(Exception e)
{
System.out.println("In catch : " + x);
}
System.out.println(x); // **5**
}
}
but in this link[URL=http://www.javaranch.com/ubb/Forum24/HTML/012797.html], author of this code says it does'nt give an error.
Saiprasad.
[This message has been edited by SaiPrasad Jukalkar (edited December 17, 2001).]
[This message has been edited by SaiPrasad Jukalkar (edited December 17, 2001).]
 
SaiPrasad Jukalkar
Ranch Hand
Posts: 79
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think this compile time error is due to System.out.println code after the return; statement.

Saiprasad.
[This message has been edited by SaiPrasad Jukalkar (edited December 17, 2001).]
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic