In the Java API documentation the constructor for FileInputStream may throw an exception when called. According to the documentation, this exception is of class FileNotFoundException. There are others listed, but these are runtime exceptions which are handled differently.
Why does the compiler generate an catch-or-declare error for exception IOException even when FileNotFoundException is caught or declare?
For example, the following code will not compile, even though FileNotFoundException is caught:
Code:
FileSystem system=FileSystems.getDefault();
Path path=system.getPath("c:\\users\\eu-A\\ToDoList.csv");
File file=path.toFile();
if(!file.exists()) {
try {
file.createNewFile();
}
catch(IOException e) {
System.exit(-1);
}
}
else {
if(file.canRead()) {
try(FileInputStream stream=new FileInputStream(file)) {
String value="";
int b;
do {
b=stream.read();
value+=(char) b;
} while(b>0);
stream.close();
}
catch(FileNotFoundException e) {
System.exit(-1);
}
}
}
The reason for this is that only FileNotFoundException is caught- but IOException is neither caught nor declared.
Why is this the case when the API documentation lists only FileNotFoundException as throwable when calling this constructor (besides the runtime exceptions which are handled differently)?
Is it my understanding of how the API documentation is presented that is at fault?
Are all exceptions that can possibly be thrown by a method given in the method's description in the API documentation?
Here is the link to the relevant section of the documentation:
https://docs.oracle.com/javase/8/docs/api/