1

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);
            }*/
        }
    }
}
2
  • 4
    In future, please include the code in your question. Commented Nov 6, 2012 at 17:33
  • Please also read more thoroughly about Java checked and unchecked expections. The first ones inherit from Exception class the latter ones from RuntimeException. Commented Nov 6, 2012 at 17:38

2 Answers 2

14

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.

Sign up to request clarification or add additional context in comments.

11 Comments

Thank You guys! OK now what should i do if i want to throw all those exceptions i mentioned in the code?
@CAD_coding: What do you mean? Your code doesn't try to throw any exceptions.
@JonSkeet i want to throw filenotfoundexception, eofexception, objectstreamexception, ioexception. found them here: today.java.net/pub/a/today/2003/12/04/exceptions.html
@Jagger actually the commented portion will be in the catch for filenotfoundexception. I want the user to know why exactly the file was not found.
@CAD_coding: If you want to throw them yourself, you can do so. Alternatively, you could do something which would throw the exception itself - such as opening a FileInputStream with the relevant file.
|
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.