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?
If you look at the manual here
delete()
only throws aSecurityException.
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.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.
Isnt the message clear? As you dont construct a
File
object, aFileNotFoundException
can never be thrown in this try block. Therefor the compilers informs you that the catch block in unneccessary.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 asIndexOutOfBoundsException
). So it will warn about attempts to catch checked exceptions that cannot arise.If you catch
Exception
, it will never complain, becauseRuntimeException
is a subtype ofException
, so your attempt will also try to catch exceptions such asIndexOutOfBoundsException
.As others have noted,
FileNotFoundException
is never thrown bydelete
. Furthermore it is a checked exception. So the Java compiler will complain.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.
Because the type of error thrown doesn't match the one you're catching. Try this...
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.