File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Un reachable statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply locked New topic
Author

Un reachable statement

SaiPrasad Jukalkar
Ranch Hand

Joined: Feb 20, 2001
Posts: 79
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Un reachable statement