Look at the docs for the File constructor you're calling. The only exception it's declared to throw is NullPointerException. Therefore it can't throw FileNotFoundException, which is why you're getting the error. You can't try to catch a checked exception which the compiler can prove is never thrown within the corresponding try block.

Creating a File object doesn't check for its existence. If you were opening the file (e.g. with new FileInputStream(...) then that could throw FileNotFoundException... but not just creating a File object.

Answer from Jon Skeet on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › io › FileNotFoundException.html
FileNotFoundException (Java Platform SE 8 )
October 20, 2025 - Java™ Platform Standard Ed. 8 ... Signals that an attempt to open the file denoted by a specified pathname has failed. This exception will be thrown by the FileInputStream, FileOutputStream, and RandomAccessFile constructors when a file with the specified pathname does not exist.
🌐
Baeldung
baeldung.com › home › java › java io › filenotfoundexception in java
FileNotFoundException in Java | Baeldung
January 25, 2024 - FileNotFoundException is a common checked exception when we work with files in Java.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › io › FileNotFoundException.html
FileNotFoundException (Java Platform SE 7 )
Java™ Platform Standard Ed. 7 ... Signals that an attempt to open the file denoted by a specified pathname has failed. This exception will be thrown by the FileInputStream, FileOutputStream, and RandomAccessFile constructors when a file with the specified pathname does not exist.
🌐
w3resource
w3resource.com › java-exercises › exception › java-exception-exercise-5.php
Java Program: File reading and empty file exception handling
The EmptyFileException class is a custom exception class that extends the base Exception class. It provides a constructor that takes a message parameter and passes it to the superclass constructor using the super keyword. If a FileNotFoundException occurs in the main method, it is caught, and an appropriate error message is printed. If an EmptyFileException occurs in the checkFileNotEmpty method, it is caught in the main method. The error message indicating an empty file is printed. ... Write a Java program to read a file and throw a custom EmptyFileException if its content is empty.
🌐
Rollbar
rollbar.com › home › how to fix the filenotfoundexception in java.io
How to Fix the FileNotFoundException in Java.io | Rollbar
September 28, 2022 - The java.io.FileNotFoundException is a checked exception in Java that occurs when an attempt to open a file denoted by a specified pathname fails.
🌐
DevQA
devqa.io › java-exception-handling
Overview of Java Exceptions and How to Handle Them
For example, when we use the FileReader class to read a file, it is essential to close the opened file at the end of processing whether an exception occurs or not. To ensure this, we can place the code to close the file inside a finally statement. import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class TryCatchFinallyBlockExample { public static void main(String[] args) { FileReader file = null; try { file = new FileReader("source.txt"); file.read(); } catch(FileNotFoundException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } finally { file.close(); } } }
🌐
LabEx
labex.io › tutorials › java-how-to-handle-file-io-exceptions-in-java-421853
How to handle file IO exceptions in Java | LabEx
Mastering file IO exception handling in Java requires a systematic approach that combines proactive error prevention, robust try-catch mechanisms, and strategic resource management.
Find elsewhere
🌐
UCI
ics.uci.edu › ~ssirani › s11_22 › CodeExamples › FileReaderExceptionsDemoWithComments.java
Exception Handling: File reader - ICS, UCI
// // An exception is thrown to indicate a failure. Elsewhere in the program, // the exception can be "caught" by code that is able to handle the problem // in some sensible way. An example of this is in the program below. import java.io.FileReader; import java.io.IOException; import ...
🌐
Medium
medium.com › javarevisited › exception-handling-in-java-89e4b292626f
Exception Handling in Java. Exception is an unwanted event which… | by Vivek Singh | Javarevisited | Medium
October 20, 2021 - void createFile() { try { File myFile = null; myFile = new File("newFile.txt"); System.out.println(myFile.getAbsolutePath()); if (myFile.createNewFile()) { System.out.println("File is created!"); } else { System.out.println("File already exists."); } } catch (Exception e) { System.out.println("An exception was thrown" + e); } } So when you call any method, and it throws an error, you will have to catch the exception either in method definition or with a try catch block.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › nio › file › FileSystemException.html
FileSystemException (Java Platform SE 7 )
7 ... AccessDeniedException, ...ryException, NotLinkException · public class FileSystemException extends IOException · Thrown when a file system operation fails on one or two files....
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › nio › file › FileSystemException.html
FileSystemException (Java Platform SE 8 )
October 20, 2025 - 8 ... AccessDeniedException, ...ryException, NotLinkException · public class FileSystemException extends IOException · Thrown when a file system operation fails on one or two files....
🌐
Ycpcs
ycpcs.github.io › cs201-fall2017 › notes › exceptionsFileIO.html
CS 201: Exceptions and File I/O
Once you understand exceptions, you will have no trouble doing input and output in Java. To read data from a file, you should create a FileInputStream object, passing a String containing the file name to FileInputStream’s constructor. The constructor for FileInputStream can throw IOException, which usually indicates that the file does not exist.
🌐
Coders Campus
coderscampus.com › home › exceptions in java
Exceptions in Java - How to Program with Java
April 9, 2021 - Anyway, like I said, the code will flow into the first catch block of code, it will then output our console message stating the file was not found, then it will re-throw the FileNotFoundException. Since there is no additional catch block to handle the re-thrown exception, we will get a nasty console error, it will look like this: Exception in thread "main" java.io.FileNotFoundException: C:aFile.txt (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(FileInputStream.java:106) at java.io.FileInputStream.<init>(FileInputStream.java:66) at java.io.FileReader.<init>(FileReader.java:41) at com.howtoprogramwithjava.fileio.FileIO.<init>(FileIO.java:15) at com.howtoprogramwithjava.runnable.MyProgram.main(MyProgram.java:11)
🌐
Coderanch
coderanch.com › t › 698237 › java › Exceptions-reading-file
Exceptions when reading from file (I/O and Streams forum at Coderanch)
When the program launches it is supposed to read "AccountData.txt" and create objects of the class Account, then add the objects to the static ArrayList "Accounts".I open the program and store some accounts,then i close it and reopen it and it throws IOException or NullPointerException(look ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-io-filenotfoundexception-in-java
java.io.FileNotFoundException in Java - GeeksforGeeks
July 23, 2025 - ... There are mainly 2 scenarios when FileNotFoundException occurs. Now let's see them with examples provided: If the given file is not available in the given location then this error will occur.
Top answer
1 of 3
1

There is no simple answer. Put yourself in the role of the user and think what they would expect, if there is a data file that is corrupted in the middle.

Let's say I have an address book with 1200 addresses, and there is one that your code cannot read. As a user, I expect to see 1199 addresses. Do I even expect an error message? I don't think so. Or at most once. Because if I use an application and every single time I search for an address I get a bloody error message I will be mightily pissed off.

Let's say my application just received a file with financial data. Say information about 217 bills that my company has to pay, and one that your code cannot read. As a user, I expect to be told that this file is corrupted, so that I can get back to the people sending the file and get a new one. Ignoring a bill that has been corrupted would be very, very bad and could lead to dire consequences.

So you see: It depends. Look at the situation and do whatever makes sense. Do it not in terms of a software developer, but from the point of view of the end user who needs the most useful results.

2 of 3
0

If you're expecting dirty data:

Dirty data is when some of the records may be dirty or not correct. The quality of data may not be high. In this case, just push the bad records into a skip file, log it and continue forward to the next one. At the end of processing, create a notification with a summary of results and note the skip file location.

This way one can process the majority of records and note the dirty ones for further remediation.

If you're expecting clean data:

Clean data means you expect every record to be valid. If the data is not clean, stop processing and create an alert. This scenario will require a pre-screen (first pass) of every record in the file to determine if it is valid or not. If the records are all valid, then the file is good and one can process. If the file is not valid, most likely the entire file is discarded and whoever/whatever is generating the file will have to create a new one to process.

Depending on your requirements, either option is acceptable.

🌐
Stack Exchange
softwareengineering.stackexchange.com › questions › 340165 › how-to-deal-with-ioexception-when-file-to-be-opened-already-checked-for-existenc
java - How to deal with IOException when file to be opened already checked for existence? - Software Engineering Stack Exchange
Suppose I have Java code that needs to open a file (see below for code). I first have a function that checks for the files existence. If the file exists, we call functions to open it and process it. Otherwise we return a message to the user stating the file could not be found. Now in the functions that open the file, we still need to have a try/catch statement for the possible IOException because it's a checked exception...
🌐
Javatpoint
javatpoint.com › filenotfoundexception-in-java
FileNotFoundException in Java - Javatpoint
FileNotFoundException in Java with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, examples etc.