Checked and unchecked exceptions in java

Checked exceptions:

Checked exceptions are those exceptions which are checked by compiler at the compile time. These exceptions will force you to either use try-catch or throws keywords. Checked exceptions include all exceptions except RuntimeException, their subclasses, and Error.

Examples: SQLException, IOExceptionetc.

import java.io.*; 
 
public class Main { 
	public static void main(String[] args) { 
		FileReader file = new FileReader("D:\\TestFiles\\file.txt"); 
		BufferedReader fileInput = new BufferedReader(file); 
 
		for (int i = 0; i < 5; i++) 
			System.out.println(fileInput.readLine()); 
 
		fileInput.close(); 
	} 
}

Output

Main.java:5: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
		FileReader file = new FileReader("D:\\TestFiles\\file.txt"); 
		                  ^
Main.java:9: error: unreported exception IOException; must be caught or declared to be thrown
			System.out.println(fileInput.readLine()); 
			                                     ^
Main.java:11: error: unreported exception IOException; must be caught or declared to be thrown
		fileInput.close(); 
		               ^
3 errors

Use throws keyword to throw the exception:

import java.io.*; 
 
public class Main { 
	public static void main(String[] args) throws IOException { 
		FileReader file = new FileReader("D:\\TestFiles\\file.txt"); 
		BufferedReader fileInput = new BufferedReader(file); 
 
		for (int i = 0; i < 5; i++) 
			System.out.println(fileInput.readLine()); 
 
		fileInput.close(); 
	} 
}

Output

Content file - line 1
Content file - line 2
Content file - line 3
Content file - line 4
Content file - line 5

Unchecked exceptions:

Unchecked exceptions represents those exceptional conditions which are not required be checked by compiler at the compile time. These are checked at run-time. These exceptions will not force you to either use try, catch or throws keyword. RuntimeException and their subclasses are unchecked exceptions. This Exception can be avoided by programmer.

Examples: ArithmeticException, NullPointerException etc.

public class Main { 
 
	public static void main(String[] args) 
	{ 
		int n1 = 15, n2 = 0; 
 
		// Try to divide by zero 
		try { 
			int result = n1 / n2; 
		} 
		catch (ArithmeticException e) { 
		    System.out.println("Exception in division operation");
		} 
	} 
}

Output

Exception in division operation

Use try catch block to handle the exception

public class Main { 
 
	public static void main(String[] args) 
	{ 
		int n1 = 15, n2 = 0; 
 
		// Try to divide by zero 
		try { 
			int result = n1 / n2; 
		} 
		catch (ArithmeticException e) { 
		    System.out.println("Exception in division operation");
		} 
	} 
}

Output

Exception in thread "main" java.lang.ArithmeticException: / by zero 
        at Main.main(Main.java:8)

Note: Both Checked and Unchecked Exception can be handled by using keyword try, catch and finally keywords.

Difference Between checked and unchecked exceptions in java

Checked ExceptionUnchecked Exception
Checked Exceptions are those exceptions which are required to be handled at compile time.Unchecked Exceptions are those exceptions which are not required to be handled at compile time.
Checked Exception represents a direct subclass of Exception.Unchecked Exceptions represents the subclass of RuntimeException.
Examples:

Checked Exceptions : NoSuchMethod, ClassNotFound.

Examples:

Unchecked Exceptions : NullPointer, IndexOutOfBounds.

Java interview questions on Exception Handling

Please Share