• 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

Return from finally

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I was trying some codes with exceptions and finally
The above code compiles but issues a warning : finally clause cannot complete normally. Also note that the function does not declare that it throws exception (from catch clause).
Can anybody please explain what's hapenning in the above code.
Regards
Vicky
[ November 19, 2003: Message edited by: Vicky Nagarkar ]
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
If you throw a exception from a method , the method signature must be defined with throws , something like this
public int m() throws Exception {}
Regards,
Yasin
 
Vicky Nag
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I am aware of that.. but the above code compiles without
"public int m() throws Exception {}" with a warning.
if i remove the return from the finally and place it at the end of the function, then it gives an error that it should be declared in the throws class.
I am not getting what's hapenning
Regards
Vicky
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
What is happening is as follows:
Your method m is throwing an exception in the try block, which is caught the catch block.
This catch block again rethrows the exception.
The rule for using a method which throws a checked exception is that either it should declare in its signature that it throws an exception or it should handle the exception within its body, and effectively not throw the exception. Now you are catching the exception and rethrowing it, so the control goes the finally block before coming out of the method.
In the finally block, by putting a return statement, you have ignored the exception that you threw in the catch block, and are providing a normal return value to be printed. Hence this value, 3 , will be printed.
Now for the second part of the question.
Had the return been put outside the finally block, as is the normal/best practice, it would have given an error saying statement not reachable because the control moves out of the method after the finally block.So the last return statement would never be reached because of the abnormal termination due to the exception you are throwing in the catch block.
Finally, I am not getting any warnings with your programs. Only errors for the conditions you said. Maybe because of different jdk versions.
Neways, I hope the flow is clear now.
Cheers
Anupreet
 
Vicky Nag
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Anupreet..
Got it now
Regards
Vicky
reply
    Bookmark Topic Watch Topic
  • New Topic