Types of Exceptions (Checked vs Unchecked)
In Java programming, program exceptions are used to control and manage program errors and other unmanageable programming conditions behaviour that may arise during a complex Java program development execution. Program exceptions in Java programming are broadly divided into two categories. Java developer can control and manage all complex errors generated in difficult programming situations in Java programming.

Checked Exceptions in Java Programming.
Checked program exceptions in Java programs are those exceptions that are tested during Java program compile time. Whereas checked Java exceptions usually occur due to external factors such as missing Java file, system network error or database connection failure. Since these errors are not developed due to programming mistakes but due to external situations. For this reason, Java developers have to control these program exceptions specially.
Key features of checked exceptions in Java programming.
Java exceptions are tested by the Java compiler at program compile time.
If a method in a Java program is capable of throwing tested exceptions, the Java compiler requires that the method either control Java program exceptions in the current program using a try-catch block or manually declare it in the method signature using the throws Java reserved keyword.
Tested exceptions in a Java program usually represent programming conditions that can cause the program to rework in some way, or program exceptions that can be controlled.
These are subclasses of the Exceptions class in a Java program, but are not the Java program runtime exceptions class.
Checked Exceptions Java program example.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class CheckedException {
public static void main(String[] args) {
try {
// it Try to open a file that doesn’t exist
FileReader file = new FileReader(new File(“nonexist.txt”));
} catch (FileNotFoundException e) {
System.out.println(“Display file Error – searh File not found”);
}
}
}
Here in this example above.
In this program, FileNotfoundtException is a tested Java program exception.
The try-catch block is used to manage and control program code exceptions, here you can also declare it in the method signature in the current program with the throws keyword.
Common checked exceptions in Java programs.
- IOException – Generally, these exceptions are generated in Java programs when input/output problems arise in the current program, such as reading a file.
- FileNotFoundException – These exceptions occur in Java programs when a particular defined file is not found in the current program.
- SQLException – These Java program exceptions occur when any error related to the database is generated in the current program.
- ClassNotFoundException – These Java program exceptions are generated when any class is not found at program runtime.
Unchecked exceptions in Java programs.
Unchecked exceptions in Java programs are exceptions that are not tested or analyzed at compile-time of a Java program, but they are tested at runtime of a Java program. Unchecked exceptions in Java are usually generated due to programming bugs or errors that are more difficult to handle in the current program, such as attempting to access an array index element outside the range of the array or dividing by zero. Remember, in Java, unchecked exceptions are subclasses of RuntimeException.
Main characteristics of unchecked exceptions in Java programs.
Unchecked exceptions in Java are not tested at program compile-time.
Unchecked exceptions do not need to be declared in the method signature or managed control with a try-catch block.
Unchecked exceptions in Java generally represent programming errors. Which cannot be re-accessed properly. Such as logical errors or invalid program input in a program.
Unchecked exceptions are subclasses of RuntimeException class.
Unchecked exception Java program example.
public class UncheckedException {
public static void main(String[] args) {
try {
int[] arr = new int[4];
// we try to check or access the array index element that doesn’t exist with ArrayIndexOutOfBoundsException
System.out.println(arr[7]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(“\n Display Error – Array index out of bounds result”);
}
}
}
Here in this example above.
ArrayIndexOutOfBoundsException in this Java program is an unchecked exception.
This unchecked exception is generated at Java program runtime. When you try to access an invalid element index location of a Java array.
Common unchecked exceptions in Java.
- NullPointerException – This is generated in Java program. When an application tries to use a null reference. Where an object is required. Such as, calling a method on a null object.
- ArrayIndexOutOfvalueException – This exception is generated. When an attempt is made to access an array index element. Which is outside the range of the currently declared array.
- ArithmeticException – This exception is generated in Java when an invalid arithmetic operation is performed. For example, division by zero.
- ClassCastException – This exception is generated when an invalid cast occurs between types.
- NumberFormatException – This exception is generated when an attempt is made to replace a string with a number, and the string does not represent a valid number.
Key Differences Between Checked and Unchecked java program Exceptions.
Feature | Checked Exceptions | Unchecked Exceptions |
Checked at Compile-time | Checked exception work and are checked at compile-time | Unchecked exception are checked only at runtime |
Handling Requirement | Checked exception explicitly handled (via try-catch) or declared in the method signature using throws keyword | Unchecked exception required to be handled or declared time |
Subclasses of | Checked Exception (not subclass of RuntimeException) | Unchecked exception display RuntimeException error |
Cause | checked exception generate Typically due to external factors (e.g., I/O, database errors) | Unchecked exception Typically caused by programming bugs (e.g., logical errors, invalid input) |
Example | Common checked exception FileNotFoundException, IOException, SQLException | Common unchecked exception NullPointerException, ArrayIndexOutOfBoundsException, ArithmeticException |
Recovery Possibility | Checked exception Can often be recovered from by the program | Unchecked exception Generally not recoverable without fixing the bug in the code |
When to use checked vs. unchecked exceptions in a program.
- Java Checked Exceptions – Apply checked exceptions when you want to manage and control remotely compatible conditions in the program code, e.g., reading a file or creating a network connection. In such conditions, it is reasonable to expect that the program can manage existing errors, e.g., prompting the user for a new file path or retrying the connection, etc.
- Java Unchecked Exceptions – Use unchecked exceptions in a Java program to manage errors or conditions that are not remotely compatible in the program, e.g., attempting to divide a number by zero or accessing an array index element from outside the range indicates a bug in the program code that typically cannot be meaningfully handled or controlled by the program at Java program runtime.