aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exceptions doubt? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exceptions doubt?" Watch "Exceptions doubt?" New topic
Author

Exceptions doubt?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9627
    
    2

Hi Guys,

The code below is from Whizlabs,



I thought it would print finally2 as the O/P. But the compiler showed, catch1, finally1, finally2???



My doubt here is that the call t.f();, the method f() throws Run time exception which is not caught...so the flow should not have gone inside the catch(Throwable e)....Am I right or wrong??


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Mark Uppeteer
Ranch Hand

Joined: Mar 02, 2004
Posts: 159

you are wrong.
finally ALWAYS runs.

regards,
Mark


I know where my towel is. (SCJP 5, OCPJWCD)
[Free Quiz Tips for a fun night with friends or family] Flash games
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9627
    
    2

Mark,

I know that finally always runs. My question here is how did it get to print catch1??
Mayur Bodkhe
Greenhorn

Joined: Nov 26, 2006
Posts: 6
In java Thowable is super class of all exception and error.
Since your are throwing new RunTimeException inside f() method, it has matching catch cause.

Now since you have both catch and finally clause, first precedence is for catch casue.

Then you are catsing Throwable object to Exception object which is valid.

also for that you have matching catch statement. so it will get executed firstly.

catch1 will be printed.
then inner finally will executed and will print finally1
and at last outer finally will get executed and will print finally 2.

so output must be
catch1
finally1
finally2
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

Throwable is superclass of all Exceptions and Errors, so it will be caught by catch block(catch(Throwable e)).

Refer Java API documentation.


Prabhu Venkatachalam<br />SCJP 1.4,SCWCD 1.4<br />prabhu.venkatachalam@gmail.com
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9627
    
    2

Thanks for the explanation.
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Jothi,
Sorry for replying after you have got the hidden trick.I have something to add in your reply..
My doubt here is that the call t.f();, the method f() throws Run time exception which is not caught...so the flow should not have gone inside the catch(Throwable e)....Am I right or wrong??

This is wrong all runtime exceptions and subclasses of Error are non-checked it simply means that they are meant to be thrown at the time when the program runs(not compiles),it never means that they are not caught when thrown explicitly or implicitly.


~Sanjeev Singh<br />SCJP 1.5
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9627
    
    2

Good effort Sanjeev. Thanks.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

when I was looking at the code I thought:"Hey, this cannot compile."

Something seemed wrong to me with the nesting. I tried out and really, this was not only due to this silly indentation (call it "exam baroque") but there was also a } bracket missing.

I find, when better indentated (ok, can't be done at exam time) the answer is easier:

Perhaps the question also wants to test, if you know, that you can rethrow an exception that is smaller than the one just caught?


Yours,
Bu.


all events occur in real time
Micheal John
Ranch Hand

Joined: Nov 01, 2006
Posts: 344
what is the purpose of rethrowing the exception? or what will rethrow exception will do?


Micheal John
SCJP 1.4 (86%), SCWCD 1.4 (86%), SCBCD 1.3 (85%), SCDJWS (Just Started...) - Satisfaction Lies in Our EFFORT, Not in the ATTAINMENT
David Grindley
Greenhorn

Joined: Oct 30, 2006
Posts: 14
Micheal John asked
what is the purpose of rethrowing the exception? or what will rethrow exception will do?


I can think of two reasons you might want to rethrow an exception:

1. Your method might need to catch the exception so that it can clean up any resources it has allocated before rethrowing the exception so that the caller of the method knows that something went wrong.

2. You may want to add some more information to any exceptions you catch in a method. So you catch the exception and extract any relevant information from it. You then create a new exception that combines information from the original exception with any extra information you want to add and then trow that exception.

All the best

Dave
Bob CHOI
Ranch Hand

Joined: Nov 10, 2006
Posts: 127
All begins with t.f()...

outer throw: RunTimeException
outer catch: upcasting RunTimeException to Throwable ->caught
inner throw: downcating Throwable to Exception
inner catch: Exception -> caught -> "catch1"
inner final: always run -> "finally1"
outer final: always run -> "finally2"

JVM sat aside even though main() declared to throw Exception, for all exceptions had been caught.


[ December 14, 2006: Message edited by: Bob CHOI ]

Hard work rewards
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exceptions doubt?