Checked and unchecked are the two types of exceptions in Java. Both of them can be handled by using try-catch block. In this way we shall see each one of in detail with the help of example code.

Checked Exception

Following are the important points note about checked exceptions:

1. Any exception class which does not have runtime exception class coming in its hierarchy is a checked exception.

2. When a method specifies a checked exception using throws keyword then the caller must either catch the exception of use throws keyword so that another another caller in the hierarchy of method calls can either catch or specify it.

3. The purpose for having checked exceptions with JDK is to force the handling of potential issues which are out of the control of the program.

4. The inclusion of checked exceptions in the JDK as the discouraged for some time now by the developers because of the fact that it forces the developer to add try-catch blocks which is frustrating. Probably JDK will come up with an alternative for checked exceptions in the near future.

5. Common examples of checked exceptions are SQLException, IOException and FileNotFoundException.

The following code shows the use of checked exceptions:

package com.example;

import java.io.File;
import java.io.FileReader;

public class Test{
 
	public static void main(String[] args) {

		File f = new File("c://text.txt");
		FileReader fw = new FileReader(f);	
	}
}

In the above code, a compiler that will be seen because if the file text the file text.txt is not present on the specified location then the file reader will fail. The error saying in this case will have the following text:

Unhandled exception type FileNotFoundException

In order to fix this compiler error the following code is required:

package com.example;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

public class Test{
 
	public static void main(String[] args) {

		File f = new File("c://text.txt");
		try {
			FileReader fw = new FileReader(f);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}	
	}
}

Un-Checked Exception

The opposite of checked exceptions are the unchecked exceptions. Following are the important points to note about unchecked exceptions:

1. All unchecked exceptions have runtime exception class is one of the class in the hierarchy of super classes.

2. These exceptions are raised in a program because of some issue with the code. Most of the times, fixing the code helps in avoiding unchecked exceptions in the application.

3. Some of the common unchecked exceptions occurring on day to day basis include null pointer exception, array index out of bounds exception and division by zero exception.

The following code shows how an unchecked exception could be raised in an application due to issues in the code:

package com.example;

public class Test{
 
	int a = 10;
	
	public static void main(String[] args) {

		Test t = null;	
		System.out.println(t.a);
	}
}

The above code compiles successfully but results in an exception while running:

Exception in thread “main” java.lang.NullPointerException
at com.example.Test.main(Test.java:11)

To fix this exception, we need to make sure that the reference variable t is properly initialized. In case it is not possible to perform checks for unchecked exception, we can use try-catch blocks for handling the exception.

Hope this tutorial clears the difference between checked and unchecked exceptions. For any questions do leave a comment.

Related Posts

Checked and Un-Checked exceptions in Java admin Core Java
Checked and unchecked are the two types of exceptions in Java. Both of them can be handled by using try-catch block. In this way we shall see each one of in detail with the help of example code. Checked Exception Following are the important points note about checked exceptions: 1. Any exception...
Checked and unchecked are the two types of exceptions in Java. Both of them can be handled by using try-catch block. In this way we shall see each one of in detail with the help of example code. <h2>Checked Exception</h2> Following are the important points note about checked exceptions: 1. Any exception class which does not have runtime exception class coming in its hierarchy is a checked exception. 2. When a method specifies a checked exception using throws keyword then the caller must either catch the exception of use throws keyword so that another another caller in the hierarchy of method calls can either catch or specify it. 3. The purpose for having checked exceptions with <a href="http://www.javaexperience.com/java-difference-between-jvm-jre-jdk-jit/" title="Difference between JVM JRE JDK JIT">JDK</a> is to force the handling of potential issues which are out of the control of the program. 4. The inclusion of checked exceptions in the JDK as the discouraged for some time now by the developers because of the fact that <strong>it forces the developer to add try-catch blocks</strong> which is frustrating. Probably JDK will come up with an alternative for checked exceptions in the near future. 5. Common examples of checked exceptions are SQLException, IOException and FileNotFoundException. The following code shows the use of checked exceptions: 1 In the above code, a compiler that will be seen because if the file text the file text.txt is not present on the specified location then the file reader will fail. The error saying in this case will have the following text: <blockquote> Unhandled exception type FileNotFoundException </blockquote> In order to fix this compiler error the following code is required: 1 <h2>Un-Checked Exception</h2> The opposite of checked exceptions are the unchecked exceptions. Following are the important points to note about unchecked exceptions: 1. All unchecked exceptions have runtime exception class is one of the class in the hierarchy of super classes. 2. These exceptions are raised in a program because of some issue with the code. Most of the times, fixing the code helps in avoiding unchecked exceptions in the application. 3. Some of the <strong>common unchecked exceptions</strong> occurring on day to day basis include <a href="http://www.javaexperience.com/java-null-value-and-nullpointerexception/" title="null value and NullPointerException">null pointer exception</a>, array index out of bounds exception and division by zero exception. The following code shows how an unchecked exception could be raised in an application due to issues in the code: 1 The above code compiles successfully but results in an exception while running: <blockquote> Exception in thread "main" java.lang.NullPointerException at com.example.Test.main(Test.java:11) </blockquote> To fix this exception, we need to make sure that the reference variable t is properly initialized. In case it is not possible to perform checks for unchecked exception, we can use try-catch blocks for handling the exception. Hope this tutorial clears the difference between checked and unchecked exceptions. For any questions do leave a comment.
Exception Handling features in JDK 7
How to generate hashcode method for a class in Java
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.

Comments

comments