Cool_Coder
Cool_Coder

Reputation: 5083

Java File Exception Handling

I am supposed to create a sample program for exception handling for file operations for my java assignment. I am having trouble understanding since I am a C++ guy. It would be really very helpful if somebody could point out the flaw in my code below. I am referring this article. Eclipse is giving me "Unreachable catch block for FileNotFoundException. This exception is never thrown from the try statement body" error.

import java.io.*;

public class file {

    public static void main(String[] args) {
        String arg1 = args[0];
        String arg2 = args[1];
        System.out.println(arg1);
        System.out.println(arg2);
        File f1, f2;

        try {
            f2 = new File(arg2);
            f1 = new File(arg1);
        }
        catch(FileNotFoundException e) {
        /*
            if(!f1.exists()) {
                System.out.println(arg1 + " does not exist!");
                System.exit(0);
            }
            if(!f2.exists()) {
                System.out.println(arg2 + " does not exist!");
                System.exit(0);
            }


            if(f1.isDirectory()) {
                System.out.println(arg1 + " is a Directory!");
                System.exit(0);
            }
            if(f2.isDirectory()) {
                System.out.println(arg2 + " is a Directory!");
                System.exit(0);
            }

            if(!f1.canRead()) {
                System.out.println(arg1 + " is not readable!");
                System.exit(0);
            }
            if(!f2.canRead()) {
                System.out.println(arg2 + " is not readable!");
                System.exit(0);
            }*/
        }
    }
}

Upvotes: 1

Views: 46477

Answers (2)

pankar
pankar

Reputation: 1701

This is because the constructor of class File with one argument

public File(String pathname)  
Parameters:pathname - A pathname string Throws: NullPointerException - If the pathname argument is null
Throws: NullPointerException - If the pathname argument is null

throws only one exception and that is NullPointerException. Your code tries to catch a FileNotFoundException which is not related to NullPointerException and this is why you get this error in Eclipse.

One way to go is to catch exceptions of class Exception which is the super class of all exceptions in Java. Another way is to catch all the exceptions (each in different catch block) that the invoked construct throws (which can be easily obtained by going through its API). The third approach is to catch only the exceptions (again which are actually thrown by the construct) that make sense to your application and ignore the others.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1503519

Look at the docs for the File constructor you're calling. The only exception it's declared to throw is NullPointerException. Therefore it can't throw FileNotFoundException, which is why you're getting the error. You can't try to catch a checked exception which the compiler can prove is never thrown within the corresponding try block.

Creating a File object doesn't check for its existence. If you were opening the file (e.g. with new FileInputStream(...) then that could throw FileNotFoundException... but not just creating a File object.

Upvotes: 14

Related Questions