This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes missing return problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "missing return problem" Watch "missing return problem" New topic
Author

missing return problem

Dan Andrei
Ranch Hand

Joined: Jan 21, 2004
Posts: 92
Given the following code:
class IO2 extends IOException{}
public int someMethod(){
int i = 0;
try{
throw new IOException(); //line 1
}
catch( IO2 e ){
//return i; (1)
}
catch( IOException e ){
return i;//(2)
}
finally{
//return i; //(3)
}
}
I get a "missing return" compilation error. I know the problem is in the IO2 catch block (if I remove it the code compiles) and it can be fixed by adding a return statement at (1) OR (3) or both, or add a return statemnt at the end of method
My question is:
I thought the rule "return correctness" is: the compiler checks every possible execution path and makes sure the methods completes abruptly on all.
if the compiler sees that an IOException is thrown in the try block, and since this is caught and a return is thrown at (2). why does it complain about ???
On an additional note first I thought a compile error will occur because an IO2 exception is never thrown for the particular catch block, but since there is a casting relation between IO2 and IOException is fine.
If IO2 was not a subclass od IOException we get a compile error


"Did anyone understand what I have just explained? ... because I did not!"
Ashok C. Mohan
Ranch Hand

Joined: Dec 03, 2003
Posts: 75
The problem here is that the compiler is not smart enough to know that the runtime path is thru the catch block of IOException.It will not try to figure out the execution path during compile time!So if there are blocks which does not have a return statements inside a method (that specifies that it returns something),the compiler will surely start shouting,at the top of its voice.


SCJP 1.4
Do not dwell in the past, do not dream of the future, concentrate the mind on the present moment.
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
Like Ashok said, the compiler ain't the brightest bear in the forest. It's like the classic debugging statement:

Should the compiler freak out if there are lines below there in the function? Yes. Will it? Sadly, no.
Moral of the story: just put in your returns. It makes your code more readable anyways. Who wants to stare as some code a year later and wonder why there's suddenly no return statement where there should be, or for that matter why there's a catch block that will never execute!


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Dan Andrei
Ranch Hand

Joined: Jan 21, 2004
Posts: 92
Thank you both for your answer...
So if a question like this pops up on the test, what should I picK;
compiler error or some output ???
Dan Andrei
Ranch Hand

Joined: Jan 21, 2004
Posts: 92
I want to make a comment to Ashok's statement:
"So if there are blocks which does not have a return statements inside a method (that specifies that it returns something),the compiler will surely start shouting,at the top of its voice. "
this not true, if i put a return in finally , it compiles even though the IO catch block has no return, so the rule above does not hold.
Ashok C. Mohan
Ranch Hand

Joined: Dec 03, 2003
Posts: 75
ok...agreed..but that's coz the compiler is sure that finally will be executed whether the catch block is executed or not.So the rule is,either the return statement should be there in all the catch blocks OR in the finally.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: missing return problem
 
Similar Threads
exception handling question
Help with catching exception
Exception and finally
Exceptions doubt??
COnfusion About Unreachable Code