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

try catch finally question

 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why does finally require a return as well ?
this is the compile error:
: Return required at end of long amethod
static long amethod()
what is the rule here ? i believe try block takes care of what the calling method return type is. and finally block is for things like "any clean-up code". (quoting from KM). nowhere does it say that finally too has to take care of the return type of the calling method.
class Test {
public static void main(String args[])
{ amethod(); }
static long amethod()
{
try{return 1;}
catch(Exception e){System.out.println("Caught Exception");}
finally{System.out.println("finally executed");}}
}
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark!
Try this code:

The error message is not about return from finally, it is from the end of the method.
If your try block runs successfully there has to be another return point from the function.
I hope this helps,
Brian
PS - Oops I forgot to mention the main point I was trying to convey. Try block has the capability to exiting abnormally/abruptly and hence the compiler tries to ensure you have a valid return statement from the method.
I also added the println statement so you can better understand what's going on.
PPS - Read my posts after 20 minutes from my original post time - this way you will get a better response. I am just learning to post at javaranch
[ February 08, 2002: Message edited by: Brian Lugo ]
[ February 08, 2002: Message edited by: Brian Lugo ]
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mark, note that you declared "amethod" to return a long value,
static long amethod()
so the compiler error was complaining that a return statement was missing! btw, the 2nd possible fix would be to declared "amethod" to have no return value like so,
static void amethod()
then you would not need the return statement.
 
machines help you to do more, but experience less. Experience this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic