This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Un reachable statement (Sorry for posting the same topic second time...) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Un reachable statement (Sorry for posting the same topic second time...)" Watch "Un reachable statement (Sorry for posting the same topic second time...)" New topic
Author

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

SaiPrasad Jukalkar
Ranch Hand

Joined: Feb 20, 2001
Posts: 79
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.

JSP
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
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

Joined: Jan 09, 2001
Posts: 1492
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

Joined: Feb 20, 2001
Posts: 79
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

Joined: Feb 20, 2001
Posts: 79
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).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Un reachable statement (Sorry for posting the same topic second time...)
 
Similar Threads
return statement effect in Exception class
Exception
Un reachable statement
Overriden exception
Why is it so