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

后端 未结 6 1325
粉色の甜心
粉色の甜心 2021-01-24 05:09

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)

相关标签:
6条回答
  • 2021-01-24 05:47

    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.

    0 讨论(0)
  • 2021-01-24 05:48

    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.

    0 讨论(0)
  • 2021-01-24 05:49

    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.

    0 讨论(0)
  • 2021-01-24 05:53

    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.

    0 讨论(0)
  • 2021-01-24 06:03

    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.

    0 讨论(0)
  • 2021-01-24 06:04

    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.

    0 讨论(0)
提交回复
热议问题