Google+

235. Handling FileNotFoundException








FileNotFoundException exception will occur when we don't provide the correct path of the file or forgot to provide the file path. Though there are many cases which throw exception when you don't provide the file path or provide the wrong file path, I am using the FileInputStream object creation statement as example to explain you the FileNotFoundException.

Lets implement this on Eclipse IDE -

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



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

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


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


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


6. Observe that the throws keyword listing the compiler suggested exception got added to the method declaration and the compiler error got resolved as shown below -


7. Now as there are no other compiler error, we can now run the Java Program code without any problems. So, Save and Run the Java class 'FileNotFoundExceptionDemo.java'  and observe that a FileNotFoundException exception got occurred, program got terminated and the exception details got displayed in the output as shown below -


8. Now add the statement that caused the FileNotFoundException exception to the try block as shown below -



9. Now add the catch block to handle the 'FileNotFoundException' exception as shown below -


10. Now write a statement after the try catch blocks to see whether the program is getting executed without getting terminated after handling the exception as shown below -


11. Save and Run the Java Class 'FileNotFoundExceptionDemo.java' and observe that the exception got handled, program got executed without terminating as shown below -


Hence we use FileNotFoundException Class in the catch block to handle the File Not Found exceptions.

As IOException Class is the parent class of FileNotFoundException Class, we can also handle the File Not Found exception using the IOException Class. Lets implement by following the below steps -

12. Replace FileNotFoundException Class in catch block with the IOException Class as shown below -


13. View the error and select 'Import IOException (java.io) ' option in the error message to resolve the error as shown below -



14. Observe that the error got resolved and an import statement got added to the Class file as shown below -


15. Save and Run the Java Class file 'FileNotFoundExceptionDemo.java' and observe that the exception got handled, rest of the program got executed without termination as shown below -



Hence you can handle the File Not Found Exception using the specific Class 'FileNotFoundException' or its parent class 'IOException'.

As all the exception classes are the child or grand child classes of the Exception and Throwable Classes. We can also use these classes to handle the exceptions. So try replacing the exception class in the catch block of the above program with Exception & Throwable classes and you will observe that the exception will be handled in the same way as we have handled it using the FileNotFoundException & IOException Classes.






Please comment below to feedback or ask questions.

Using finally in exception handling will be explained in the next post.





No comments: