Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

missing return statement

Status
Not open for further replies.

kanghao

IS-IT--Management
Jul 4, 2004
68
KR
I think the method1 and the method2 is the same.
Why the compiler complains "missing return statement" in the method2?
Thanks.

public class ExceptionHandlingTest {
public static void main(String[] args) throws Exception {
System.out.println(method1());
}
public static String method1() throws Exception{
try{
return "method1";
}catch(Exception e){
throw new Exception();
}finally{

}
}
public static String method2() throws Exception{
try{
return "method2";
}catch(Exception e){
method3();
}finally{

}
}
public static void method3() throws Exception{
throw new Exception();
}
}
 
Because method2 must return something no matter which path it takes through the code in it. If the catch block is executed, there is no path out of the method using a return, a fact which the compiler correctly informs you of.

Code:
public static String method2() throws Exception{
        try{
            return "method2";
        }catch(Exception e){
            method3();
            return "method3";
        }
    }
...should work.

...and in your code, the finally is redundant.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
I guess the compiler is not checking, if method3 throws allways an exception - which might be possible, but which is such an unusual behaviour, that it will only evaluate the signature of the method, which claims it will void, not a String.
It's better to confuse programmers, who write such suspicious code, than build a compiler with suspicious-checkings, which will perform lousy, and get new bugs from that feature.

seeking a job as java-programmer in Berlin:
 
As far as I'm aware, the compiler only verifies things like this based on paths of execution. Anything more would require the code to be executed; Exceptions could be thrown depending upon tortuous logic which in turn could depend upon user input to the program - no way the compiler would attempt to handle verifications to that level.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
The difference beetween method1 and method2 are only execution paths, no logic involved.

I guess it's more based on signatures, as stefan said. The compiler can ckech wether an exception is thrown inside the code and mark that path as "valir for every return type", but as soon as another method is called, it just checks the return type of that method.

Anyway, I think that if I were the compiler I'd cry on my kneees with these methods.

Cheers,

Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top