This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
    Bookmark Topic Watch Topic
  • New Topic

Un reachable statement

 
SaiPrasad Jukalkar
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
    Bookmark Topic Watch Topic
  • New Topic