0

when i attempt to create a BufferedReader using a text file in my workspace the FileReader in the "new bufferedreader statement throws a filenotfoundexception. yet .exists() and .canRead() both return true for the file

public static void main(String[] args) {
    File fighter0 = new File("resources/fighter0.txt");
    //BufferedReader reader = new BufferedReader(new FileReader(fighter0));
    System.out.println(fighter0.exists());
    System.out.println(fighter0.canRead());
}

here's the code

true
true

and the output

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Unhandled exception type FileNotFoundException

at main.Core.main(Core.java:21)

and the exception that's thrown when i uncomment the bufferedreader line

i found a closed thread similar to this one, but couldn't find any clear answer in it.

user112233
  • 41
  • 5
irksomesloth
  • 35
  • 1
  • 6

2 Answers2

3

This is a compiler error, not a runtime failure:

Unhandled exception type FileNotFoundException

The compiler is stating that that exception must be caught by the client code. It can be corrected by implementing a try/catch around the use of FileReader or by stating that main() throws FileNotFoundException.

Research checked and unchecked exceptions.

hmjd
  • 120,187
  • 20
  • 207
  • 252
0

Your error is compilation error. It says that you have not handled the FileNotFoundException exception. Try public static void main(String[] args) throws Exception

G.S
  • 10,413
  • 7
  • 36
  • 52