Google+

236. Using finally in exception handling








finally keyword is used in a try catch block statement to specify a block of code which gets executed regardless of whether the exception is thrown or not thrown.

Lets implement this on Eclipse IDE -

1. Launch Eclipse IDE, create a new java class 'FinallyDemo.java' with main( ) method in the existing project as shown below -



2. Create a try catch block to handle any exception using Exception Class as shown below -


3. Now add finally block to the try catch blocks as shown below -


4. Write statements which you want to get executed irrespective of whether the exeception is thrown or not thrown as shown below -



5. Save and Run the Java class file 'FinallyDemo.java' and observe that the print statement inside the finally block got executed, even after the exception is thrown from try block and handled by the catch block as shown below -


6. Now change the statement in the try block, such that it wont throw any exception as shown below -


7. Save and Run the Java class file 'FinallyDemo.java' and observe that the print statement inside the finally block got executed, even after the exception is not thrown from try block as shown below -



Hence code in the finally block will be executed irrespective of the try catch blocks throwing or handling exceptions.

The main usage of the finally block is to do clean up job.  Keeping cleanup code in a finally block code is always a good practice, even when no exceptions are occurred. The runtime system always executes the code with in the finally block regardless of what happens in the try block. So it is ideal place to clean up code.

Lets take an example of FileInputStream Class. When you create an object for FileInputStream Class, the object refers to a resource file. After using the resource in the program code, we must clean up the resource explicitly at the end by calling the close( ) method. But when we place our code in try catch blocks, we have to add finally block at the end to clean up the resources.


Lets implement this on Eclipse IDE -

1. Launch Eclipse IDE, create a new Java Class 'FinallyDemo2.java' with main( ) method in the existing Java Project 'Project 46' as shown below -



2. Create an object for FileInputStream Class by providing a valid path of a file as shown below -


3. View the error and select 'Import FileInputStream (java.io) ' option from the error message as shown below -



4. Observe that the import statement got added and the error got resolved as shown below -



5. View the error displayed after resolving the above error and select 'Surround with try/catch' option as shown below -


6. FileInputStream object creation statement got surrounded with try catch blocks and the error got resolved as shown below -


7. Now add a finally block to above try catch block to clean up the resource created by FileInputStream Class using the close( ) method as shown below -



8. View the error as shown below and observe that the object 'fis' of FileInputStream Class is not identified in the finally block as shown below -


9. Now make the FileInputStream object 'fis' recognized by finally block by moving the declaration part of the 'fis' object creation statement from the try block, outside the blocks as shown below -


10. View the compiler error that is displayed after resolving the above error and select 'Add throws declaration' option from the error message to resolve the error as shown below -



11. Observe that the throws keyword listing IOException got added to declaration part of the main( ) method and the error got resolved as shown below -



12. Now view the other error displayed after resolving the above error as shown below and select 'Initialize variable' option from the error message as shown below -


13. Observe that fis object got initialized with null parameter and the error got resolved as shown below -


This is the proper way of cleaning up the data by write the statements in the finally block.

14. Now lets add a statement inside the finally block to print the whether the finally block is executed as shown below -


15. Save and Run the Java Class file 'FinallyDemo2.java' and observe that the statements in the finally block got executed and the result is printed in the output as shown below -


Hence the statements in the finally block got executed and using the above example we have used finally block to close the resources by using close( ) method.






Please comment below to feedback or ask questions.

Using finally block immediately after the try block will be explained in the next post.







No comments: