How to handle checked exceptions

Generally, checked exceptions occurs when application code dealing with external resources like file, database, network connection, servers etc. So checked exceptions don’t raise because of programming mistake, They raise because of external resources unavailability. So we can’t avoid checked exceptions. But we have to handle them and define an alternative way when checked exceptions raised. Let us discuss the with the following example.

Example 01 : checked exception – checked by compiler – compilation error

Compilation Errors :

Example01CheckedException.java:15: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
fis = new FileInputStream(“demo.txt”);
^
Example01CheckedException.java:19: error: unreported exception IOException; must be caught or declared to be thrown
while ((c = fis.read()) != -1) {
^
Example01CheckedException.java:24: error: unreported exception IOException; must be caught or declared to be thrown
fis.close();
^
3 errors

In the above example, We are reading the file demo.txt and displaying its content on the console. We got three compilation errors as we mentioned in the comments of the program. We can fix the above compilation errors in two ways as shown below.

Example 02 : checked exception – declare exception with throws keyword

In the above example, We are declared both IOException and FileNotFoundException on main() method signature to avoid compilation errors. We can also declare like public static void main(String args[]) throws IOException. Because IOException is super class for FileNotFoundException. But it always advisable to declare all possible exceptions instead super class of all the possible exceptions.

Declaring exceptions on main() method is not advisable because it is final place where we should handle exceptions. If this program fails to read file content, it leads to abnormal termination of the program. So the remaining code which is not related file reading also won’t execute.

We can declare exceptions on methods(not main() method) where ever you feel that, the caller method would take decision what need to be done when exception is raised.

For example Consider constructor FileInputStream(File filename) throws FileNotFoundException. Here they are not handling exception in constructor, Just they are declaring exception. If we have a requirement that, if ‘demo.txt’ not exist then we need to read ‘sample.txt’. If FileInputStream constructor handled exception with try catch block, Then how would caller knows whether the file loading is success or not? So declaring exception is required in some cases.

Example 03 : checked exception – handle exception with try-catch

In the above example, We are handling the exception with try-catch block. It runs fine and display the content if file exist and reading passed. If file doesn’t exist or reading failed, then the above program clearly show the reason for failure and avoids abnormal termination of the program so that if there is any remaining code that will execute smoothly.

Scroll to Top