• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

missing return problem

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Dan Andrei
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic