This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptions doubt?

 
Joe Harry
Ranch Hand
Posts: 10105
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Mark Uppeteer
Ranch Hand
Posts: 159
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are wrong.
finally ALWAYS runs.

regards,
Mark
 
Joe Harry
Ranch Hand
Posts: 10105
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark,

I know that finally always runs. My question here is how did it get to print catch1??
 
Mayur Bodkhe
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 502
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Throwable is superclass of all Exceptions and Errors, so it will be caught by catch block(catch(Throwable e)).

Refer Java API documentation.
 
Joe Harry
Ranch Hand
Posts: 10105
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation.
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joe Harry
Ranch Hand
Posts: 10105
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good effort Sanjeev. Thanks.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Micheal John
Ranch Hand
Posts: 344
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the purpose of rethrowing the exception? or what will rethrow exception will do?
 
David Grindley
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic