Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

silly but severe problem

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello everybody
here is the problem.
if u remove the comments for the last "return " statement it compiles.
but it is the against the basic funda. No method can have two return statements which r executable under any circumstances.
for the same program if u comment the "try and catch" ,it says "statement not reached" which is absolutely fine and it should be the case even if the try-catch block is present as try-catch block is not an obstacle for any statement for execution.
i am enclosing the code for ur convenience and appreciate any solution.
thanks
Reddy

[I added UBB CODE tags to your source code to make it more readable. Please try to use
them in the future. Learn more about UBB codes here - Ajith ]

[This message has been edited by Ajith Kallambella (edited October 13, 2000).]
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are quite right that you can't have two return statements that can be reached under any circumstances, but your return true statement will only be reached if an Exception is not thrown first.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I commented System.out.println from try block and keeps both the return statements (without comments), still it compiles cleanly. I think return statement in try block doesn't guaranteed to be executed always and so considered conditional/optional.
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Here in your code you are assuming that the statement in the try block goes through smoothly and in your case it does but if you replace your line
System.out.println(x+"-one");
With the line
System.out.println(1/0);
then what happens,the statement goes to the corresponding catch block and if you don't have a return false there then how can your method return a boolean..think about this just replace the line and then you will understand the flow of logic by yourself.
Surya
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler is behaving as it should since the first return statement is inside a try block. If you remove the try statement and the catch block, so that both the return statements occur under the same block, then you get an error message at the second return statement since it is unreachable. So, the critical factor here is to note if the two return statements occur within the same block.
hope this helps.
Srikrish
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is how I fix your code. You have to understand how try/catch works.
public boolean check(String x)
{
try{
System.out.println(x+"-one");
}catch(Exception e)
{
System.out.println("in catch");
return false;
}
System.out.println("here0");
return true;

}
}
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic