This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "About Exception Handling" Watch "About Exception Handling" New topic
Author

About Exception Handling

ahmad razaa
Greenhorn

Joined: Jan 03, 2011
Posts: 10
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]
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.
OCPJP6/SCJP6 86%
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281


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


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
Hrishikesh Yeshwant Alshi
Ranch Hand

Joined: Dec 06, 2010
Posts: 62

@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.
 
wood burning stoves
 
subject: About Exception Handling
 
Similar Threads
Exception Handling
Awkward Question from Exception
try/catch??
Answer this correctly and dont worry about Exceptions Flow Control
Awkward Question from Exception