What is the real use of finally block in try-catch-finally?
(Anything inside finally executes anyway..right?!)
If an exception is caught in a catch block,anyway the contents/lines below the block is going
to execute..Then what is the specific need of finally block?
Well what if you are rethrowing the exception that you have caught then i don't think any line below the catch block will run. But finally will always run even if you are rethrowing the exception that has been caught.
[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Yeh the finally block would usually be used some cleanup code.
For example if you are reading from a database or from a file you would want to ensure that the connections to the file/database are closed.
You would want to do this if the application completes normally or if there was an exception thrown... so in this case you would use the finally block.
Well yeah closing a particular application is a good clean up but we have to see the closing method does not throw a checked exception which will land another try catch in your finally block. This happens in the file operations when you try to put the close method in the finally block without handling or declaring the checked exception. So we need to keep special watch on the clean up codes that we put in the finally block.
thank you all...thanks for all your nice explanations..!
so is it ok if we put another try-catch-finally inside finally....because connection.close() - clean up requires exception to be caught or thrown..