Win a copy of Hello World this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Round up question #132

 
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?
 
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
 
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
 
Story like this gets better after being told a few times. Or maybe it's just a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!