0
0
JavaComparisonBeginner · 4 min read

Checked vs Unchecked Exception in Java: Key Differences and Usage

In Java, checked exceptions are exceptions that the compiler forces you to handle explicitly, either by catching or declaring them. Unchecked exceptions are runtime exceptions that the compiler does not require you to handle, often caused by programming errors.
⚖️

Quick Comparison

Here is a quick table summarizing the main differences between checked and unchecked exceptions in Java.

AspectChecked ExceptionUnchecked Exception
DefinitionExceptions checked at compile-timeExceptions checked at runtime
HandlingMust be caught or declaredNo requirement to catch or declare
Common SuperclassException (excluding RuntimeException)RuntimeException and its subclasses
CauseExternal factors (e.g., file not found)Programming errors (e.g., null pointer)
ExamplesIOException, SQLExceptionNullPointerException, ArithmeticException
Compiler EnforcementYesNo
⚖️

Key Differences

Checked exceptions are exceptions that the Java compiler forces you to handle explicitly. This means you must either catch them with a try-catch block or declare them in the method signature using throws. They usually represent conditions outside the program's control, like missing files or network errors.

On the other hand, unchecked exceptions are subclasses of RuntimeException. The compiler does not require you to handle or declare them. These exceptions often indicate programming mistakes, such as accessing a null object or dividing by zero.

Because checked exceptions require explicit handling, they help make your code more robust by forcing you to think about error conditions. Unchecked exceptions are used for errors that are usually bugs and should be fixed in code rather than caught.

⚖️

Code Comparison

java
import java.io.*;

public class CheckedExceptionExample {
    public static void main(String[] args) {
        try {
            readFile("nonexistent.txt");
        } catch (IOException e) {
            System.out.println("Caught checked exception: " + e.getMessage());
        }
    }

    static void readFile(String filename) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(filename));
        System.out.println(reader.readLine());
        reader.close();
    }
}
Output
Caught checked exception: nonexistent.txt (No such file or directory)
↔️

Unchecked Exception Equivalent

java
public class UncheckedExceptionExample {
    public static void main(String[] args) {
        String text = null;
        try {
            System.out.println(text.length());
        } catch (NullPointerException e) {
            System.out.println("Caught unchecked exception: " + e);
        }
    }
}
Output
Caught unchecked exception: java.lang.NullPointerException
🎯

When to Use Which

Choose checked exceptions when you expect the caller to handle recoverable conditions, such as file I/O errors or database issues. They make error handling explicit and improve program reliability.

Choose unchecked exceptions for programming errors that should be fixed in code, like null references or illegal arguments. These exceptions indicate bugs and usually should not be caught but corrected.

In summary, use checked exceptions for external, recoverable problems and unchecked exceptions for internal, programming mistakes.

Key Takeaways

Checked exceptions must be caught or declared; unchecked exceptions do not require this.
Checked exceptions represent recoverable conditions; unchecked exceptions represent programming errors.
Use checked exceptions to force handling of expected problems like I/O failures.
Use unchecked exceptions for bugs that should be fixed, not caught.
Understanding the difference helps write clearer and more reliable Java code.