Checked vs Unchecked Exceptions in Java
Last Updated :
19 Jul, 2024
In Java, Exception is an unwanted or unexpected event, which occurs during the execution of a program, i.e. at run time, that disrupts the normal flow of the program’s instructions.Â
In Java, there are two types of exceptions:
- Checked exceptions
- Unchecked exceptions

Checked Exceptions in Java
These are the exceptions that are checked at compile time. If some code within a method throws a checked exception, then the method must either handle the exception or it must specify the exception using the throws keyword. Checked exceptions can be fully checked or partially checked.
- Fully Checked Exception: A checked exception where all its child classes are also checked (e.g.,
IOException, InterruptedException). - Partially Checked Exception: A checked exception where some of its child classes are unchecked (e.g.,
Exception).
Checked exceptions represent invalid conditions in areas outside the immediate control of the program (like memory, network, file system, etc.). Any checked exception is a subclass of Exception. Unlike unchecked exceptions, checked exceptions must be either caught by the caller or listed as part of the method signature using the throws keyword.
Example:
Java
// Java Program to Illustrate Checked Exceptions
// Where FileNotFoundException occurred
// Importing I/O classes
import java.io.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Reading file from path in local directory
FileReader file = new FileReader("C:\\test\\a.txt");
// Creating object as one of ways of taking input
BufferedReader fileInput = new BufferedReader(file);
// Printing first 3 lines of file "C:\test\a.txt"
for (int counter = 0; counter < 3; counter++)
System.out.println(fileInput.readLine());
// Closing file connections
// using close() method
fileInput.close();
}
}
Output:Â

To fix the above program, we either need to specify a list of exceptions using throws, or we need to use a try-catch block. We have used throws in the below program. Since FileNotFoundException is a subclass of IOException, we can just specify IOException in the throws list and make the above program compiler-error-free.
Example:
Java
// Java Program to Illustrate Checked Exceptions
// Where FileNotFoundException does not occur
// Importing I/O classes
import java.io.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
throws IOException
{
// Creating a file and reading from local repository
FileReader file = new FileReader("C:\\test\\a.txt");
// Reading content inside a file
BufferedReader fileInput = new BufferedReader(file);
// Printing first 3 lines of file "C:\test\a.txt"
for (int counter = 0; counter < 3; counter++)
System.out.println(fileInput.readLine());
// Closing all file connections
// using close() method
// Good practice to avoid any memory leakage
fileInput.close();
}
}
Output:Â
First three lines of file "C:\test\a.txt"
Unchecked Exceptions in Java
These are the exceptions that are not checked at compile time. In C++, all exceptions are unchecked, so it is not forced by the compiler’s to either handle or specify the exception. It is up to the programmers to be civilized and specify or catch the exceptions. In Java, exceptions under Error and RuntimeException classes are unchecked exceptions, everything else under throwable is checked.Â
Consider the following Java program. It compiles fine, but it throws ArithmeticException when run. The compiler allows it to compile because ArithmeticException is an unchecked exception.
Example:
Java
// Java Program to Illustrate Un-checked Exceptions
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Here we are dividing by 0
// which will not be caught at compile time
// as there is no mistake but caught at runtime
// because it is mathematically incorrect
int x = 0;
int y = 10;
int z = y / x;
}
}
Output
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Main.main(Main.java:5)
Java Result: 1
In short unchecked exceptions are runtime exceptions that are not required to be caught or declared in a throws clause. These exceptions are usually caused by programming errors, such as attempting to access an index out of bounds in an array or attempting to divide by zero.
Unchecked exceptions include all subclasses of the RuntimeException class, as well as the Error class and its subclasses.
The separation into checked and unchecked exceptions sounded like a good idea at the time. However, over the years, it has introduced more boilerplate and less aesthetically pleasing code patterns than it solved real problems. The typical (and unfortunately quite cumbersome) pattern within the Java ecosystem is to hide (or wrap) the checked exception within an unchecked one.
Example:
Java
try {
// Some I/O operation here
} catch( final IOException ex ) {
throw new RuntimeException( "I/O operation failed", ex );
}
Here are some examples of unchecked exceptions in Java:
1. ArrayIndexOutOfBoundsException: This exception is thrown when you attempt to access an array index that is out of bounds.
2. NullPointerException: This exception is thrown when you attempt to access a null object reference.
3. ArithmeticException: This exception is thrown when you attempt to divide by zero or perform an invalid arithmetic operation.
Conclusion
Checked exceptions in Java must be either caught or declared in the method signature, representing conditions outside the program’s control. Unchecked exceptions, typically caused by programming errors, are not required to be caught or declared. Understanding the difference between these exceptions helps write more robust and error-free code.
Please Login to comment...