Exceptions in Java are events that interrupt the normal flow of program execution. They are broadly categorized into Checked Exceptions and Unchecked Exceptions, based on when they are detected and how they are handled by the compiler and runtime environment.
- Checked Exception: These exceptions are checked at compile time, forcing the programmer to handle them explicitly.
- Unchecked Exception: These exceptions are checked at runtime and do not require explicit handling at compile time.
Checked Exceptions in Java
Checked exceptions are exceptions that are checked by the compiler at compile time. If a method can throw a checked exception, it must be either handled using a try-catch block or declared using the throws keyword.
- Derived from the Exception class (excluding RuntimeException and its subclasses).
- Help ensure that exceptional conditions are anticipated and handled properly.
- Common examples include IOException, SQLException, and ClassNotFoundException.
Types of Checked Exception
Checked exceptions in Java are categorized based on the type of problem they represent, and they must be either handled using a try-catch block or declared using the throws keyword.
- 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).
Commonly Occurred Checked Exceptions
- IOException : Occurs when an input/output operation fails, such as reading from or writing to a file.
- SQLException : Occurs when there is an error while interacting with a database.
- ClassNotFoundException : Thrown when the JVM cannot find a specified class at runtime.
- FileNotFoundException : Occurs when a program attempts to access a file that does not exist.
- InterruptedException : Thrown when a thread is interrupted while waiting, sleeping, or performing certain operations.
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.
import java.io.*;
class Geeks
{
public static void main(String[] args)
{
// Getting the current root directory
String root = System.getProperty("user.dir");
System.out.println("Current root directory: " + root);
// Adding the file name to the root directory
String path = root + "\\message.txt";
System.out.println("File path: " + path);
// Reading the file from the path in the local directory
FileReader f = new FileReader(path);
// Creating an object as one of the ways of taking input
BufferedReader b = new BufferedReader(f);
for (int counter = 0; counter < 3; counter++)
System.out.println(b.readLine());
f.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.
import java.io.*;
class Geeks {
public static void main(String[] args) throws IOException {
// Getting the current root directory
String root = System.getProperty("user.dir");
System.out.println("Current root directory: " + root);
// Adding the file name to the root directory
String path = root + "\\message.txt";
System.out.println("File path: " + path);
// Reading the file from the path in the local directory
try {
FileReader f = new FileReader(path);
// Creating an object as one of the ways of taking input
BufferedReader b = new BufferedReader(f);
// Printing the first 3 lines of the file
for (int counter = 0; counter < 3; counter++)
System.out.println(b.readLine());
f.close();
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
} catch (IOException e) {
System.out.println("An I/O error occurred: " + e.getMessage());
}
}
}
Output:

Note: If you are running this code on Linux/macOS, use the correct path format i.e. /home/user/test/a.txt
Explanation: In the above program we create a Java program which reads a file from the same directory this program may throw exceptions like FileNotFoundException or IOException so we handle it using the try-catch block to handle the exceptions and execute the program without any interruption.
Unchecked Exceptions in Java
Unchecked exceptions are exceptions that are not checked by the compiler at compile time. They occur during program execution and usually result from programming errors or incorrect logic.
- They are subclasses of RuntimeException.
- Handling them is optional and not enforced by the compiler.
- Common examples include NullPointerException, ArithmeticException, and ArrayIndexOutOfBoundsException.
Note: In Java, exceptions under RuntimeException and errors under Error are considered unchecked.
Types of Unchecked Exceptions in Java
Java provides several types of unchecked exceptions that occur during program execution and are not required to be handled by the compiler.
Commonly Occurred Unchecked Exceptions:
- NullPointerException : Occurs when attempting to access a method or field on a null reference.
- ArithmeticException : Thrown for illegal arithmetic operations, such as division by zero.
- ArrayIndexOutOfBoundsException : Occurs when accessing an array with an invalid index.
- StringIndexOutOfBoundsException : Thrown when a string is accessed using an invalid index.
- NumberFormatException : Occurs when converting an invalid string to a numeric type.
- ClassCastException : Thrown when an object is cast to an incompatible type.
Consider the following Java program. It compiles fine, but it throws an ArithmeticException when run. The compiler allows it to compile because ArithmeticException is an unchecked exception.
class Geeks {
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:

Note :
- Unchecked exceptions are runtime exceptions that are not required to be caught or declared in a throws clause.
- These exception are caused by programming errors, such as attempting to access an index out of bounds in an array or attempt 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. Over the years, it has introduced more boilerplate and less aesthetically pleasing code patterns than it solved real problems. The typical pattern within the Java ecosystem is to hide the checked exception within an unchecked one.
Example:
try {
// Some I/O operation here
} catch( final IOException ex ) {
throw new RuntimeException( "I/O operation failed", ex );
}
Checked Exception vs Unchecked Exceptions
Checked Exception | Unchecked Exception |
|---|---|
Checked exceptions are checked at compile time. | Unchecked exceptions are checked at run time. |
Derived from Exception. | Derived from RuntimeException. |
Caused by external factors like file I/O and database connection cause the checked Exception. | Caused by programming bugs like logical errors cause unchecked Exceptions. |
Checked exceptions must be handled using a try-catch block or must be declared using the throws keyword | No handling is required. |
Examples: IOException, SQLException, FileNotFoundException. | Examples: NullPointerException, ArrayIndexOutOfBoundsException. |