FileNotFoundException IOException

FileNotFoundException IOException

A few methods in Java API cannot be executed by the JVM alone. JVM takes the help of the underlying operating system to do the job. Such methods are known as native methods. For example, for all the file operations and thread operations, the JVM should communicate with the operating system (where the Java program is being executed) and with the operating system’s co-operation only, the task intended will be achieved. File management and thread management etc. are managed entirely by the OS. Any language should communicate and take the help of OS.

Any problem in the communication (known as communication gap) and any programmer's task is not done successfully, the JVM informs through an exception and especially as a checked exception. The name of the checked exception is included in the method signature or constructor signature. Some checked exceptions with constructor signatures are given hereunder.

Two examples of checked exception are FileNotFoundException IOException very often used in file operations. Many file copying methods throw IOException. Observe the following two methods signature of FileInputStream class.

Both the methods read() and close() throw a checked exception called FileNotFoundException. While using these methods, these methods must be placed in try-catch blocks; else program does not compile.

Following is the hierarchy.

Object –> Throwable –> Exception –> IOException –> FileNotFoundException

Full hierarchy of exceptions is available at "Hierarchy of Exceptions – Checked/Unchecked Exceptions".

The following program uses these two methods and handles FileNotFoundException IOException.

The program is given to stress more on FileNotFoundException and IOException. The explanation (like FileInputStream, read() and close() etc.) of the above code is available at "Semantics of File Copying".

Why the FileInputStream constructor throws FileNotFoundException?

The job of FileInputStream constructor is to open the file abc.txt in read mode. The programmer opens the file and goes on writing some file manipulation code. Writing such code is waste when the file does not exist at all. The wise job is first check whether the file exist or not and then proceed on writing the code. This warning is notified by the Java through FileNotFoundException. As this is a checked exception, it must be handled by the programmer else program does not compile; here it is handled with try-catch block.

Why the read() and close() methods throw IOException?

We know the file operations like read, write and close() etc. are OS dependent and are declared as native methods. In the communication, the OS may not read properly or write properly and also close properly. Even though these failures are very rare, the designers of Java did not take a chance (to make Java a robust language) and informs the programmer through IOException. As this is a checked exception, it must be handled by the programmer else program does not compile; here it is handled with try-catch block. IOException is a general exception which indicates some input output exception and is thrown by many methods of stream classes.

The above code can be modified where the exceptions are handled with throws keyword instead of try-catch block; but this style is not robust way and used in rough or sample code development.

The above main() method can be still modified as follows.

Instead of throwing two exceptions only one is thrown. We know in exception handling, super class exception can handle subclass exception also.

One thought on “FileNotFoundException IOException

  1. Abdulelah Fallatah

    I enjoyed reading your article. It helped me confirm that I understood there are two ways to handle exceptions, albeit the latter way (I mean the way that doesn’t use the catch and try blocks) is not encouraged.

    Thank you for confirming the idea I had about exception handling.

Leave a Reply

Your email address will not be published. Required fields are marked *