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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Un reachable statement

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Too many men are afraid of being fools - Henry Ford. Foolish tiny ad:
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
    Bookmark Topic Watch Topic
  • New Topic