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  silly but severe problem Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark " silly but severe problem" Watch " silly but severe problem" New topic
Author

silly but severe problem

shyam reddy
Greenhorn

Joined: Oct 13, 2000
Posts: 1
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).]
Graeme Brown
Ranch Hand

Joined: Oct 13, 2000
Posts: 193
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.
Kalpesh Pandya
Greenhorn

Joined: Oct 07, 2000
Posts: 14
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.
Surya Bahadur
Ranch Hand

Joined: Sep 28, 2000
Posts: 88
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
srikrish
Ranch Hand

Joined: Sep 12, 2000
Posts: 63
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
bante
Greenhorn

Joined: Jun 19, 2000
Posts: 11
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;

}
}
 
 
subject: silly but severe problem
 
Threads others viewed
Why is it so
Un reachable statement
code following the finally block.
Return
Un reachable statement (Sorry for posting the same topic second time...)
MyEclipse, The Clear Choice