Reputation: 739
At Java 8 documentation, it is written that the constructor from Scanner which receives a File Source as parameter throws a FileNotFoundException
.
But take a look at the following code:
try{
sc = new Scanner("Rede.txt"); //This archive already exists
}
catch(FileNotFoundException f){
f.printStackTrace;
}
finally{
sc.close();
}
When I run it, I get something like:
error:exception FileNotFoundException is never thrown in body of corresponding try statement
catch(FileNotFoundException f){
Same happens with IOException
. The curious is that, if I throw away the try-catch part, the code compiles.
What is wrong here?
Upvotes: 0
Views: 52
Reputation: 37875
Scanner can also scan a String. To see what I mean, try:
System.out.println( new Scanner("Rede.txt").next() );
It will print Rede.txt
.
Some other classes (like e.g. FileInputStream) will take a String path, but Scanner doesn't. If you want to use a file, you need to actually pass it a File:
sc = new Scanner(new File("Rede.txt"));
Upvotes: 4