Reputation: 214
I am confused currently as to why I am receiving "unreachable catch block for FileNotFoundException." when I try to run my code. I am taking in a file path from the main method arguments, and catching the error in the instance that the input path is wrong or that the file cannot be found in the path.
Could someone please give me a hand with this? Here's my code for this part:
public void readFile(String inputFilePath, String outputFilePath) throws IOException{
StringBuilder sb = new StringBuilder();
File input = null;
try{
input = new File(inputFilePath);
}
catch (FileNotFoundException e){
System.err.println("Input file cannot be found in the provided path");
}
Upvotes: 1
Views: 2725
Reputation: 27078
Because this line
input = new File(inputFilePath);
doesn't throw FileNotFoundException
If you dig into the code of new File(..)
this is what it has
public File(String pathname) {
if (pathname == null) {
throw new NullPointerException();
}
this.path = fs.normalize(pathname);
this.prefixLength = fs.prefixLength(this.path);
}
as you can see this method doesn't throw FileNotFoundException
, there is only possibility of NPE.
If you were to extend your code to read the file like this
new BufferedInputStream(new FileInputStream(input));
then FileNotFoundException
makes sense. Try it out.
Upvotes: 3