Checked Exception v/s Unchecked Exception

In this article, we will discuss checked and unchecked exception in detail with explanation & examples and also list some of most commonly known checked & unchecked exception

Checked Exception:

  • Exception which are checked at compile-time during compilation is known as Checked Exception
  • Alternate definition: any line of code that could possibly throw exception, and if it is raised to handle during compilation is said to be checked exception
  • For example, accessing a file from remote location could possibly throw file not found exception
  • It is the programmer’s responsibility to handle the checked exception for successful compilation
  • This way, if any exception is raised during execution then respective handling code will be executed
  • Note: if it isn’t handled then program will throw compile-time error
  • Example: IOException, FileNotFoundException, InterruptedException, SQLException, etc
  • Except Runtime exception & its child classes and error & its child classes, all other exception falls under the category of Checked Exception

CheckedException.java

package in.bench.resources.exception.handling;

import java.io.BufferedReader;
import java.io.FileReader;

public class CheckedException {

	public static void main(String[] args) {

		FileReader fileReader = new FileReader(
				"F:\\BenchRes.txt");
		BufferedReader bufferedReader = 
				new BufferedReader(fileReader);    

		// logic for reading     
	}
}

Output:

Compile-time error: Unhandled exception type FileNotFoundException

Checked exception screen-capture from Eclipse IDE:

Possible solution:

  • Provide/surround with try-catch block or
  • add throws clause in method signature at the end

 

Unchecked Exception:

  • Exception which are NOT checked at compile-time is known as Unchecked Exception
  • Alternate definition: any line of code that could possibly throw exception at runtime is said to be unchecked exception
  • Unchecked exception are because of programming-error
  • For example, accessing out of index-position to assign some value during execution could possibly throw exception at runtime
  • So, it is again programmer’s responsibility to handle unchecked exception by providing alternate solution in the exception handling code
  • Note: if it isn’t handled properly then program will terminate abnormally at runtime
  • Example: Runtime exception & its child classes and error & its child classes are examples of Unchecked Exception
  • Like ArithmeticException, NullPointerException, NumberFormatException, ArrayIndexOutOfBoundsException, StatckOverflowError, etc

UncheckedException.java

package in.bench.resources.exception.handling;

public class UncheckedException {

	public static void main(String[] args) {

		char[] ch = new char[4];
		ch[7] = 'B';

		System.out.println(ch);
	}
}

Output:

Exception in thread "main" 
java.lang.ArrayIndexOutOfBoundsException: 7
	at in.bench.resources.exception.handling
	.UncheckedException.main(UncheckedException.java:8)

Unchecked exception screen-capture from Eclipse IDE:

Possible solution:

  • First of all, find out what possible Exception/Error program can be thrown during execution
  • Then handle those exception either by surrounding with try-catch block or adding throws declaration at the end of method signature, as per business/programming convenience

 

Misconception about checked and unchecked exception:

  • Sometimes, checked exception are also referred as compile-time exception and unchecked exception are referred as runtime exception
  • But this is mis-leading because every exception (whether it is checked or unchecked) occurs/raised only at the runtime i.e.; during program execution only
  • Reason: during compilation; checked exception are caught and raises compile-time error, due to which programmer has to handle the exception by providing either try-catch blocks or using throws keyword
  • Whereas unchecked exception aren’t caught during compilation, rather it raises exception during execution because of programming error

 

References:

 

Read Also:

 

Happy Coding !!
Happy Learning !!

try-catch block in Java
Exception Hierarchy in Java