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

2020-04-27 04:54发布

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?

6条回答
手持菜刀,她持情操
2楼-- · 2020-04-27 05:27

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.

查看更多
小情绪 Triste *
3楼-- · 2020-04-27 05:29

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.

查看更多
干净又极端
4楼-- · 2020-04-27 05:29

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.

查看更多
等我变得足够好
5楼-- · 2020-04-27 05:37

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.

查看更多
兄弟一词,经得起流年.
6楼-- · 2020-04-27 05:41

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.

查看更多
beautiful°
7楼-- · 2020-04-27 05:48

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.

查看更多
登录 后发表回答