What are Checked and Unchecked Exceptions in Java?
Java exceptions are categorized into Checked (compile-time) and Unchecked (runtime) exceptions.
1. Checked Exceptions
Checked at compile time. Must be handled using try-catch or declared with throws.
import java.io.*;
public class CheckedExceptionExample {
public static void main(String[] args) {
try {
FileReader file = new FileReader("test.txt");
BufferedReader br = new BufferedReader(file);
System.out.println(br.readLine());
br.close();
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
} catch (IOException e) {
System.out.println("IO Error: " + e.getMessage());
}
}
}
// Alternative: Using throws declaration
class CheckedWithThrows {
public static void readFile() throws IOException {
FileReader file = new FileReader("test.txt");
BufferedReader br = new BufferedReader(file);
System.out.println(br.readLine());
br.close();
}
}
Common Checked Exceptions
- IOException - Input/Output operations
- SQLException - Database access errors
- ClassNotFoundException - Class not found
- InterruptedException - Thread interruption
2. Unchecked Exceptions (Runtime Exceptions)
Not checked at compile time. Occur due to programming errors.
public class UncheckedExceptionExample {
public static void main(String[] args) {
// ArithmeticException
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
}
// NullPointerException
String str = null;
try {
System.out.println(str.length());
} catch (NullPointerException e) {
System.out.println("String is null");
}
// ArrayIndexOutOfBoundsException
int[] arr = new int[3];
try {
arr[5] = 10;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Index out of bounds");
}
// NumberFormatException
try {
int num = Integer.parseInt("abc");
} catch (NumberFormatException e) {
System.out.println("Invalid number format");
}
}
}
Common Unchecked Exceptions
- NullPointerException - Accessing null reference
- ArithmeticException - Division by zero
- ArrayIndexOutOfBoundsException - Invalid array index
- NumberFormatException - Invalid number conversion
- IllegalArgumentException - Invalid argument passed
Custom Exception Examples
Custom Checked Exception
class InsufficientBalanceException extends Exception {
public InsufficientBalanceException(String message) {
super(message);
}
}
class BankAccount {
double balance = 1000;
void withdraw(double amount) throws InsufficientBalanceException {
if (amount > balance) {
throw new InsufficientBalanceException("Insufficient balance. Available: " + balance);
}
balance -= amount;
System.out.println("Withdrawn: " + amount + ", Remaining: " + balance);
}
}
Custom Unchecked Exception
class InvalidAgeException extends RuntimeException {
public InvalidAgeException(String message) {
super(message);
}
}
class Voter {
void checkAge(int age) {
if (age < 18) {
throw new InvalidAgeException("Age " + age + " is not valid for voting");
}
System.out.println("Valid voter");
}
}
Comparison Table
| Feature | Checked Exceptions | Unchecked Exceptions |
|---|---|---|
| Checked at | Compile time | Runtime |
| Handling required | Yes (try-catch or throws) | Optional |
| Parent class | Exception (except RuntimeException) | RuntimeException |
| Recoverable | Often recoverable | Usually programming errors |
Master exception handling with Online Learner and write robust Java applications!
0
likes
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
