Low Orbit Flux Logo 2 F

Java How To Handle Filenotfoundexception

The Java exception “FileNotFoundException” means exactly what it sounds like. It means that the file that you were attempting to work with doesn’t exist or can’t be accessed. We are going to show you how to handle this exception. You are generally going to need to handle this any time you call a method that throws this exception.

Quick Answer: Here is what the syntax looks like:


        try {
            ....
        } catch( FileNotFoundException e1 ){
           ....
        } catch( IOException e2 ) { 
           ....
        }

Here is an actual example. Note that we also catch another exception “IOException” since it also needs to be handled. Generally any code that throws FileNotFoundException will also throw IOException.


import java.io.*;

public class Test1 {
    public static void stuff() {
        try {
        BufferedReader rd = new BufferedReader(new FileReader(new File("test12345.txt")));
        rd.readLine();
        } catch( FileNotFoundException e1 ){
                System.out.println( "Error - Can't find file: " + e1 );
        } catch( IOException e2 ) { 
                System.out.println( "Error - Can't find file: " + e2 );
        }
    }
    public static void main( String[] args ){
            stuff();
    }
}

If the file doesn’t exist the output will look like this:


Error - Can't find file: java.io.FileNotFoundException: test12345.txt (No such file or directory)

You can actually leave out catching FileNotFoundException since it would be handled by calling catching IOException. Catching both of these gives you the option to use different code depending on which exception is caught. We catch the more specific exception first and the less specific exception second. The first exception could be thought of as a subtype of the second.

You can actually just catch all exceptions in one shot if you wanted. The most generic exception is actually just called “Exception”.

The following example will actually catch both exceptions and any other exception that could be thrown. It isn’t as bad as it sounds since you can actually print out the exception message with the variable “e”.


try {
    BufferedReader rd = new BufferedReader(new FileReader(new File("test12345.txt")));
    rd.readLine();
} catch( Exception e ) {
    System.out.println( "Error - " + e );
}

If you want to avoid catching exceptions all together you can specify that your method throws the exception. This will result in whatever code calls your code having to catch the exception.

You can throw the generic “Exception” which will include all exceptions like this:


public static void stuff() throws Exception {

You can also specify which exceptions you want to throw like this:


public static void stuff() throws FileNotFoundException, IOException {

Here is an example showing how we can specify that our method “stuff()” effectively throws all exceptions. Our main method is the calling method so it will need to either handle these or throw them itself. We choose to also throw these exceptions from the main method. This results in us not having to ever actually catch them at all.


import java.io.*;
  
public class Test1 {
    public static void stuff() throws Exception {
        BufferedReader rd = new BufferedReader(new FileReader(new File("test12345.txt")));
        rd.readLine();
    }
    public static void main( String[] args ) throws Exception{
            stuff();
    }
}

If the file doesn’t exist the output will look like the following. It basically just prints the exception message to the command line.


Exception in thread "main" java.io.FileNotFoundException: test12345.txt (No such file or directory)
       at java.base/java.io.FileInputStream.open0(Native Method)
       at java.base/java.io.FileInputStream.open(FileInputStream.java:211)
       at java.base/java.io.FileInputStream.<init>(FileInputStream.java:153)
       at java.base/java.io.FileReader.<init>(FileReader.java:75)
       at Test1.stuff(Test1.java:5)
       at Test1.main(Test1.java:9)