• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

About Exception Handling

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a confusion about this code.


This code is giving the results:

1
4
Java.lang.RuntimeException

As we handled the RuntimeException , then why its throwing the exception and why its not run finally block?

Early answer will be appreciated.

[Edit: added code tags and indented code - Ankit]
 
Ranch Hand
Posts: 62
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For my own sanity:

 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Yeshwant,

I have re formatted your code for better understanding




Here you see you have just one "try" block and all catch block and finally is for that only , there is no " try" block to catch RuntimeException and that's why its not getting caught and after printing what's there in finally its terminating.


If you modify code like below you will be able to catch RuntimeException.



Thanks
Javin
 
Hrishikesh Yeshwant Alshi
Ranch Hand
Posts: 62
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ahmad razaa.

Javin explains it correctly.

@Javin,

This question was asked by ahmad razaa. I was about to post the same reply but you did it just before me. You explanation makes it very clear.
 
I have gone to look for myself. If I should return before I get back, keep me here with this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic