Java has a unique class of exceptions called checked exceptions. These exceptions are also called compile-time exceptions, because they are caught when the code is compiled, rather than when an error occurs, like most other exceptions.
1. Introduction
Consider the following Java code:
import java.util.Scanner; public class UncheckedExceptionExample { public static void main(String[] args) { // Collect an integer from the user. Scanner sc = new Scanner(System.in); System.out.print("Enter a number between 0 to 2: "); int idx = sc.nextInt(); // Use the integer to retrieve a value from the array below. int[] arr = {10, 20, 30}; // Produces an error if the user enters anything outside of 0 to 2. System.out.println(arr[idx]); } }
If the user enters a value outside of 0 to 2 when running it, it will cause an ArrayIndexOutOfBoundsException
. Otherwise, the code runs completely fine.
The code below, however, cannot run under any circumstances, even if the file exists:
import java.io.File;
import java.io.FileReader;
public class CheckedExceptionExample {
public static void main(String[] args) {
// Even if the file exists, this code does not run.
File file = new File("C:/existing-file.txt");
FileReader fr = new FileReader(file);
}
}
This is because the constructor for FileReader
throws a checked exception. Hence why you see the error below if you try running the code.
java: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
If you need to test your Java code, you can use this online Java compiler here.
2. Handling checked exceptions
So, how do we handle checked exceptions? If your line(s) of code contains a checked exception, you need to either:
- Catch the checked exception in your code, or;
- Throw the checked exception upwards
Below, we explore the 2 ways we can fix the above-mentioned checked exception example.
Article continues after the advertisement:
a. Catching the checked exception in your code
In the example below, the FileReader
constructor throws a checked FileNotFoundException
(see the definition here). Hence, we put the line into a try-catch block and catch the exception.
import java.io.File;
import java.io.FileReader;
public class CheckedExceptionExample {
public static void main(String[] args) {
try {
// Even if the file exists, this code does not run.
File file = new File("C:/existing-file.txt");
FileReader fr = new FileReader(file);
} catch (FileNotFoundException e) {
System.out.println("C:/existing-file.txt does not exist.");
}
}
}
Instead of a FileNotFoundException
, you can also catch an IOException
in the code above, since that is one of the parent classes of the FileNotFoundException
.
b. Throw the checked exception upwards
The easier (and lazier way) is to make your own caller function throw a checked exception itself:
import java.io.File;
import java.io.FileReader;
public class CheckedExceptionExample {
public static void main(String[] args) throws FileNotFoundException {
// Even if the file exists, this code does not run.
File file = new File("C:/existing-file.txt");
FileReader fr = new FileReader(file);
}
}
This means that any functions calling it will need to handle the checked exception. If all the functions above also throw the exception (as in our example), then this will basically make the code behave like an unchecked exception, i.e. it only occurs if an exception actually occurs.
3. Identifying checked exceptions
To identify which exceptions are checked, you will need to consult the Java API. Specifically, you want to check the method headers of the methods that you are using, and see if these methods use the throws
keyword, as shown above.
You can also use the throws
keyword on your own methods to make them throw checked exceptions.
4. Conclusion
I hope that after studying this article, you have a better grasp on what checked exceptions are. Checked exceptions are somewhat controversial, as some developers have spoken out on them being one of the many bad features found in Java. If you’re interested in studying more about them, check out the articles below: