permaculture playing cards
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About Exception Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "About Exception Handling" Watch "About Exception Handling" New topic

About Exception Handling

ahmad razaa

Joined: Jan 03, 2011
Posts: 10
I have a confusion about this code.

This code is giving the results:


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]
Hrishikesh Yeshwant Alshi
Ranch Hand

Joined: Dec 06, 2010
Posts: 62

For my own sanity:

Write your code as if the person who maintains it is a homicidal maniac who knows where you live.
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 295

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.


SQL Query Interview Question java67 Javarevisited
Hrishikesh Yeshwant Alshi
Ranch Hand

Joined: Dec 06, 2010
Posts: 62

@ahmad razaa.

Javin explains it correctly.


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 agree. Here's the link:
subject: About Exception Handling
It's not a secret anymore!