Resolve Unreported Exception IOException Must Be Caught or Declared to Be Thrown in Java
-
Demonstration of Unreported
IOException
-
Use
try-catch
to Catch UnreportedIOException
-
Use the
throws
Keyword to EradicateIOException

This tutorial demonstrates another compile time exception saying unreported exception ioexception; must be caught or declared to be thrown
. We will also learn about its possible causes and solutions via sample programs.
Demonstration of Unreported IOException
Example Code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class TestClass {
public static void main(String[] args){
BufferedReader inputFile = null;
PrintWriter outputFile = null;
String inputFileName = "C:\\New folder\\inputFile.txt";
String outputFileName = "C:\\New folder\\outputFile.txt";
try {
inputFile = new BufferedReader(new FileReader(inputFileName));
outputFile = new PrintWriter(new FileWriter(outputFileName));
String lineOfText = inputFile.readLine();
while (lineOfText != null) {
if (lineOfText.contains("x")) {
lineOfText = lineOfText.replaceAll("x" + ".*", "\"Updated\"");
}
outputFile.println(lineOfText);
lineOfText = inputFile.readLine();
}
} catch (IOException ioe) {
System.err.println("Caught IOException: " + ioe.getMessage());
} finally {
if (inputFile != null) {
inputFile.close();//<------This line causes this exception
}
if (outputFile != null) {
outputFile.close();
}
}
}
}
In the code above, we read data from the specified input file; look for the letter x
. As soon as it is found, we replace the x
and the upcoming text on the same line with the word Updated
enclosed in double quotes (" "
).
Finally, we close both files (input and output).
The line inputFile.close();
that we are pointing to in the above code example is causing an unreported IOException
which must be caught or thrown. What does this mean?
It means the inputFile.close();
is causing the IOException
that we can get rid of using the try-catch
block (it’s called catching the exception) or using the throws
keyword (it’s called exception is thrown). How to do it?
Let’s learn with code examples below.
Use try-catch
to Catch Unreported IOException
Example Code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class TestClass {
public static void main(String[] args){
BufferedReader inputFile = null;
PrintWriter outputFile = null;
String inputFileName = "C:\\New folder\\inputFile.txt";
String outputFileName = "C:\\New folder\\outputFile.txt";
try {
inputFile = new BufferedReader(new FileReader(inputFileName));
outputFile = new PrintWriter(new FileWriter(outputFileName));
String lineOfText = inputFile.readLine();
while (lineOfText != null) {
if (lineOfText.contains("x")) {
lineOfText = lineOfText.replaceAll("x" + ".*", "\"Updated\"");
}
outputFile.println(lineOfText);
lineOfText = inputFile.readLine();
}
} catch (IOException ioe) {
System.err.println("Caught IOException: " + ioe.getMessage());
} finally {
if (inputFile != null) {
try{
inputFile.close();
}catch(IOException ioe){
System.err.println("Caught IOException: " + ioe.getMessage());
}
}
if (outputFile != null) {
outputFile.close();
}
}
}
}
The input file has the following content.
this is x one file
this is x two file
this is x three file
After executing the program, we get an output file containing the content below.
this is "Updated"
this is "Updated"
this is "Updated"
Here, we eliminate the IOException
using the try-catch
block, where the try
statement lets us define a specific block of code that needs to be examined for errors during the execution process.
If any exception occurs at any particular statement within the try
block, it will stop execution and jump to the catch
block. So, it is strongly advised not to keep the code within the try
block that does not throw any exception.
On the other hand, the catch
statement permits defining a block of code that needs to be executed if there is any error in the try
block. The catch
is written right after the try
block.
We can also have multiple catch
blocks based on how many exceptions we can get in the respective try
block.
We can also write a customized message while handling the exception. Now, the question is how these exceptions are handled.
The Java Virtual Machine (JVM) checks whether an exception is handled or not. If it is not, the JVM serves with the default exception handler, which does a few tasks that are listed below:
- It prints the description of an exception in the program’s output console.
- It also prints the stack trace in the program’s output console. The stack trace is the methods’ hierarchy where an exception occurred.
- It results in the program’s termination.
On the other side, the application’s normal flow is maintained if an application programmer has handled the exception, which means the remaining code gets executed.
Use the throws
Keyword to Eradicate IOException
Example Code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class TestClass {
public static void main(String[] args) throws IOException{
BufferedReader inputFile = null;
PrintWriter outputFile = null;
String inputFileName = "C:\\New folder\\inputFile.txt";
String outputFileName = "C:\\New folder\\outputFile.txt";
try {
inputFile = new BufferedReader(new FileReader(inputFileName));
outputFile = new PrintWriter(new FileWriter(outputFileName));
String lineOfText = inputFile.readLine();
while (lineOfText != null) {
if (lineOfText.contains("x")) {
lineOfText = lineOfText.replaceAll("x" + ".*", "\"Updated\"");
}
outputFile.println(lineOfText);
lineOfText = inputFile.readLine();
}
} catch (IOException ioe) {
System.err.println("Caught IOException: " + ioe.getMessage());
} finally {
if (inputFile != null) {
inputFile.close();
}
if (outputFile != null) {
outputFile.close();
}
}
}
}
The content of an input file is as follows.
this is x one file
this is x two file
this is x three file
The output file has the updated content as given below.
this is "Updated"
this is "Updated"
this is "Updated"
This code is the same as the previous section, where we only use the try-catch
block to handle the exception. Here, we are using throws
to declare an exception; we can also declare multiple exceptions separated by a comma (,
).
The throws
keyword informs the application programmer that an exception may occur in this method. Remember, the throws
keyword is written while defining the function (see in the given example code).
So, it is good and strongly advised that the programmer handle this exception in the code to maintain the normal execution flow. Otherwise, the program will terminate. Let’s understand it via example code.
Copy the following code and run it. Make sure we have an incorrect path for the input file.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class TestClass {
public static void main(String[] args) throws IOException{
BufferedReader inputFile = null;
PrintWriter outputFile = null;
String inputFileName = "C:\\New folder\\input.txt";
String outputFileName = "C:\\New folder\\outputFile.txt";
try {
inputFile = new BufferedReader(new FileReader(inputFileName));
outputFile = new PrintWriter(new FileWriter(outputFileName));
String lineOfText = inputFile.readLine();
while (lineOfText != null) {
if (lineOfText.contains("x")) {
lineOfText = lineOfText.replaceAll("x" + ".*", "\"Updated\"");
}
outputFile.println(lineOfText);
lineOfText = inputFile.readLine();
}
} catch (IOException ioe) {
System.err.println("Caught IOException: " + ioe.getMessage());
} finally {
if (inputFile != null) {
inputFile.close();
}
if (outputFile != null) {
outputFile.close();
}
}
System.out.println("Continue execution!");
}
}
OUTPUT:
Caught IOException: C:\New folder\input.txt (The system cannot find the file specified)
Continue execution!
We will see the output as given above. The program continues code execution because we have handled the exception.
Now, comment out the try
, catch
, and finally
blocks as follows and rerun the code. Make sure we have an incorrect path for an input file.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class TestClass {
public static void main(String[] args) throws IOException{
BufferedReader inputFile = null;
PrintWriter outputFile = null;
String inputFileName = "C:\\New folder\\input.txt";
String outputFileName = "C:\\New folder\\outputFile.txt";
//try {
inputFile = new BufferedReader(new FileReader(inputFileName));
outputFile = new PrintWriter(new FileWriter(outputFileName));
String lineOfText = inputFile.readLine();
while (lineOfText != null) {
if (lineOfText.contains("x")) {
lineOfText = lineOfText.replaceAll("x" + ".*", "\"Updated\"");
}
outputFile.println(lineOfText);
lineOfText = inputFile.readLine();
}
//} catch (IOException ioe) {
// System.err.println("Caught IOException: " + ioe.getMessage());
//} finally {
if (inputFile != null) {
inputFile.close();
}
if (outputFile != null) {
outputFile.close();
}
//}
System.out.println("Continue execution!");
}
}
The program will terminate the execution as soon as it finds the exception and never reaches the following line of code.
System.out.println("Continue execution!");
This is the reason we handle our declared exceptions. Remember, we can only declare the checked
exceptions because the programmer can handle the unchecked exceptions within the code.
Related Article - Java Error
- Fix the Error: Failed to Create the Java Virtual Machine
- Fix the Missing Server JVM Error in Java
- Fix the 'No Java Virtual Machine Was Found' Error in Eclipse
- Fix the Error: Failed to Create the Java Virtual Machine
- Javax.Net.SSL.SSLHandShakeException: Remote Host Closed Connection During Handshake
- Java.Lang.VerifyError: Bad Type on Operand Stack