dwwilson66
dwwilson66

Reputation: 7066

Why am I getting errors with my Java try...catch?

I'm starting to teach myself more about Java error handling, and this is my first program where I'm trying to see specific errors instead of using catch (Exception e) as a generic catch-all catch.

I'm deleting a file and returning a message that the file was deleted successfully or that the deletion was a failure. In cases where the deletion fails, I want to deliver an error message if the file is not found.

Here's where I am so far:

public static void deleteOldConcatFiles(File concatFile) {
    try
    {
        if(concatFile.delete()) {
            System.out.println(concatFile.getName() + " is deleted!");
        } else {
            System.out.println("Delete operation failed.");
        }
    }
     //
    catch(FileNotFoundException f) {
        System.out.println("Exception: "+ f.getMessage().getClass().getName());
    }
}

When I run that, I'm getting a warning: This this is an unreachable catch block for FileNotFoundException. This exception is never thrown from the try statement body.

However, when I run with THIS catch block,

catch(Exception f) {
     System.out.println("Exception: "+e.getMessage().getClass().getName());
}

I get no error or warning message.

Why is this happening, and what can I do to fix it?

Upvotes: 2

Views: 1474

Answers (6)

Pedantic
Pedantic

Reputation: 5022

If you look at the manual here delete() only throws a SecurityException.

Also, it returns a boolean value which indicates whether or not the file was deleted. This should be all the information needed to indicate to the user if everything worked out.

Upvotes: 1

Ross Drew
Ross Drew

Reputation: 8246

Because the type of error thrown doesn't match the one you're catching. Try this...

catch(Exception e) {
  System.out.println("Exception: "+ e.getClass());
}

That will show you the type of error you should be catching. Obviously this isn't good practice but it's a good exercise for seeing what's happening. Other answers on this page concerning checked and unchecked exceptions are pretty concise.

Upvotes: 3

Alex North
Alex North

Reputation: 1209

File.delete() does not throw FileNotFoundException, even if the file does not exist.

FileNotFoundException is a checked exception (i.e., not a RuntimeException), so any code that throws it must declare it. Because File.delete() does not declare that it throws FileNotFoundException, the compiler guarantees that it won't, and can promise that your catch block will never be invoked.

The second, catch-all block does not generate a warning because it also catches RuntimeExceptions (RuntimeException extends Exception), which the compiler does not check for you. Thus, it might be invoked, the compiler isn't sure, so it doesn't warn you.

Upvotes: 7

creichen
creichen

Reputation: 1768

Java supports two kinds of exceptions: checked exceptions (statically checked) and unchecked exceptions (RuntimeException and its subtypes).

The Java compiler can tell at compile time whether a checked exception (such as FileNotFoundException) can be thrown or can definitely not be thrown. It can't tell that for unchecked exceptions (such as IndexOutOfBoundsException). So it will warn about attempts to catch checked exceptions that cannot arise.

If you catch Exception, it will never complain, because RuntimeException is a subtype of Exception, so your attempt will also try to catch exceptions such as IndexOutOfBoundsException.

As others have noted, FileNotFoundException is never thrown by delete. Furthermore it is a checked exception. So the Java compiler will complain.

Upvotes: 3

Zeus
Zeus

Reputation: 6566

Look for IOException. or deleteifExist method if you are not interested in the exception, if you want to retrn something, then file.exists() will help you fgure if the file is there or not.

Upvotes: 0

Sebastian Hoffmann
Sebastian Hoffmann

Reputation: 11482

Isnt the message clear? As you dont construct a File object, a FileNotFoundException can never be thrown in this try block. Therefor the compilers informs you that the catch block in unneccessary.

Upvotes: 0

Related Questions