(#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?
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.
Joined: Apr 30, 2001
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
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.