Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Round up question #132

 
Thiru Narayanan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(#132) TRUE or FALSE: if an exception is not caught, the finally block will run and the rest of the method is skipped.
Answer: TRUE
The finally block will always run if an exception is thrown, and then the exception is immediately passed to the calling method.
My Doubt : The question talks about "exception is not caught". But the answer talks about "exception is thrown"
Can somebody please clarify?
 
Annie Weaver
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a finally block, first you must have a try block where the exception is thrown from.
If there is no matching catch block, or no catch block at all, the exception is not caught in that method, but rather thrown to the calling method.
Any code following where the exception was thrown is skipped, up to the finally block. The finally block executes, then you go back to the calling method with the exception.
 
Thiru Narayanan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Annie.
Can anybody please clarify one more point.
If a value is returned from the 'finally' block, when the exception is not caught what will happen?
THIRU
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Thiru,
Anything that gets executed in the finally block will change the output of the method. If you decide to send a good return value then you will override the uncaught exception with a good value. Also if your finally method throws another exception and that is also uncaught it will then override the original uncaught exception and be thrown from the method. The following code example show how the finally block can send back good data which nullifies the uncaught exception throw.

Regards,
Manfred.
 
Thiru Narayanan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Manfred,
That is really useful.
THIRU
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic