Prepare for your Next Interview
This is a discussion on Purpose of FINALLY Block within the Java forums, part of the Software Development category; What is the purpose of finally block (i know Mandatory statements are written in finally block) but outside of the catch block statements are also executed right i write mandatory ...
|
|||
|
Purpose of FINALLY Block
What is the purpose of finally block (i know Mandatory statements are written in finally block) but outside of the catch block statements are also executed right i write mandatory statements are written out side the catch block. so pls tell me finally block need?
Question asked by visitor Sudheer |
| Sponsored Links |
|
|||
|
Quote:
The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated. |
|
|||
|
Re: Purpose of FINALLY Block
The Main purpose of Finally Block is to close all the files that are opened in the code or any datebases in the code.Here(in Finally Block)we write code to close all datebases ,files etc.
The Finally Block will Execute Wheteher there is an Exception or not |
|
|||
|
Re: Purpose of FINALLY Block
finally block
- it handles uncatched exceptions. - this must be followed with the try and catch block. - if try block throw an exception then no other catch block handle that exception. now finally block executes that exception. example: try { //it throws a zero by division error } catch(NullPointerException) { // it throws NullPointerException instead of Zero by division error } finally{ printStackTrace(); } - in the above example no catch statement to handle the exception i.e throw by the try then finally block can handle that exception by using the printstacktrace() method. printstacktrace(), which handles any other exption. main advantage is to be finally block must be executed. note:dont enter any statement between try,catch and finally block even System.out.print() also. if u use compiler gives an error |
| The Following User Says Thank You to rahulvegi For This Useful Post: | ||
|
|||
|
Quote:
1) please note that the finally block will ALLWAYS be executed, no matter what. not just if an exception was not caught. 2) what happends if an exception is thrown from within the finally block? - aha: the execution of the finally code will stop and the exception will be thrown out of the current function! |
|
|||
|
Re: Purpose of FINALLY Block
finaly block is executed whether or not an exception accures.it will be executed always .The
main purpose of this is to execute the code that we have to execute in every condition. for example we have to close a database connection whether our command executes succesfully or not Try { myComm.executeNonQuery(); } catch(Ex) { //do something with exception Ex return; } finaly { myConn.close(); } the main point to see in above code that whether we have applied a return statement in catch block finaly block will still execute. |
|
|||
|
Purpose of finally block :
--> The finally block is self explanatory(finallyyyyyy). --> The piece of code which you want to execute after come out of statements in try block must and should must be written in finally block. i.e independent of reslut of try bolck code whether it is successful or not . Ex : public class FinallyTest { FinallyTest() { String str = "1"; try { //try to get array or stringIndexOutOfBoundsException System.out.println(str.charAt(2)); } catch(NumberFormatException ne) { ne.printStackTrace(); System.out.println("catch called"); } finally { System.out.println("finally called"); } System.out.println("method end called"); } public static void main(String args[]) { FinallyTest objTest1 = new FinallyTest(); } } --> Your questiona about after catch block statments also will be executed the why finally , Consider above example, In that method StringIndexOutOdBOundsException occured which is not caught. so statements below catch cannot be executed. But statments in finally are executed. The statments below catch block are executed only if you caught the raised exception. But the statements in finally block are executed even though you caught or uncaught the raised exception --> So now I think You Got IT. |
|
|||
|
Actually out side the catch block u can write the mandatory codes but when an exception occurred before the mandatory statement then these codes are not
executed so in a program finally block are execute, whether there is some exception occur or not, so the mandatory statement must execute. |
|
|||
|
Hi,
Follow the snippest code here try { // do something here } catch { // If any errors are there then the control will return from here without //executing next code after this catch block } to do some operation if any exception is thrown from the try block you need finally { // do other oprations } |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Anonymous Block | krishnaindia2007 | Oracle | 4 | 07-26-2008 12:32 PM |
| What is the purpose of 'if not itab is initial' | Geek_Guest | SAP R/3 | 1 | 02-01-2008 03:41 AM |
| what is the purpose of using UNIX in software testing ? please explain in details | fkrgullu | Unix/Linux | 1 | 06-16-2007 04:48 PM |
| Purpose of testing | sakshi_2801 | Testing Issues | 3 | 02-27-2007 08:04 AM |
| Is this command serve the purpose of renaming | christia | Unix/Linux | 1 | 09-09-2006 04:18 PM |