The short answer

Checked exceptions are checked by the compiler, so Java forces you to either catch them or declare them with throws; they extend Exception and usually mean a recoverable problem like a missing file. Unchecked exceptions are not checked at compile-time, so handling is optional; they extend RuntimeException and usually mean a bug, like a null reference. In short, checked is enforced and recoverable, while unchecked is optional and signals a programming error.

Exceptions in Java split into checked exceptions and unchecked exceptions. Both appear in every Java and interview syllabus. Understanding the difference is crucial for writing robust, error-handling code, yet beginners often mix up which ones the compiler forces you to handle.

The core question is when Java checks the exception. Does the compiler demand handling before the program runs, or not? Checked exceptions take the first path, while unchecked exceptions take the second. This guide defines each type, shows the hierarchy and the code, compares them in detail, and explains when to use which.

If you are still mapping out Java basics, it also helps to know the difference between method overloading and overriding.

Class hierarchy tree with Throwable at the top branching to Exception and Error, Exception branching to Checked and RuntimeException, and RuntimeException branching to Unchecked
Checked exceptions extend Exception; unchecked exceptions extend RuntimeException.

What are Checked Exceptions?

Checked exceptions are exceptions that are checked at compile-time. This means the compiler forces you to either handle the exception with a try-catch block or declare it with the throws keyword in the method signature. So you cannot ignore a checked exception and still compile.

In the hierarchy, a checked exception is a subclass of Exception but not of RuntimeException. It usually marks a recoverable condition, such as a file that is missing or a network link that drops. Common examples are IOException, SQLException, and FileNotFoundException.

Advantages of checked exceptions:

  • Forces developers to handle exceptions, which leads to more robust code.
  • Improves readability by naming the exceptions a method can throw.
  • Encourages clean, deliberate error handling for expected failures.

Disadvantages of checked exceptions:

  • Can lead to verbose code with many try-catch blocks.
  • May force handling in methods where the exception rarely occurs.

What are Unchecked Exceptions?

Unchecked exceptions, also known as runtime exceptions, are not checked at compile-time. So Java does not require you to catch or declare them. These exceptions typically result from programming errors, and the code compiles whether or not you handle them.

In the hierarchy, an unchecked exception is a subclass of RuntimeException. It usually marks a bug rather than a recoverable event. Common examples are NullPointerException, ArrayIndexOutOfBoundsException, and ArithmeticException.

Advantages of unchecked exceptions:

  • Simplifies code, since you need not handle exceptions that are unlikely.
  • Helps surface programming errors during development.
  • Allows faster development through less restrictive error handling.

Disadvantages of unchecked exceptions:

  • Can cause unexpected crashes if they are never handled.
  • May hide real issues in the code when they go unaddressed.

The Exception Hierarchy

Every Java exception sits under one root class, so the tree explains the whole split.

At the top is Throwable. It has two children: Exception and Error. Under Exception sit the checked exceptions, plus one special subclass, RuntimeException. Under RuntimeException sit the unchecked exceptions. So the rule is short: if a class extends RuntimeException, it is unchecked; if it extends Exception but not RuntimeException, it is checked. Error, meanwhile, marks serious failures like OutOfMemoryError, and it is also unchecked.

Checked vs Unchecked Exceptions: Comparison Table

Comparison figure with two columns for checked and unchecked exceptions listing checked when compile-time versus runtime, handling required versus optional, extends Exception versus RuntimeException, and means recoverable versus bug
Checked vs unchecked exceptions at a glance: when checked, handling, parent class, and meaning.
AspectChecked ExceptionsUnchecked Exceptions
HandlingMust be handled at compile-time with try-catch or throwsNot mandatory to handle or declare at compile-time
Parent classSubclass of Exception (not RuntimeException)Subclass of RuntimeException
Compiler ruleForces developers to handle or declare in the method signatureDevelopers are not compelled to handle
ExamplesIOException, SQLException, FileNotFoundExceptionNullPointerException, ArrayIndexOutOfBoundsException
When checkedChecked at compile-time (compile-time exceptions)Not checked at compile-time (runtime exceptions)
Hierarchy noteDescends from Throwable via ExceptionDescends from Throwable via RuntimeException
NatureConsidered recoverableUsually unexpected; indicate programming errors
IntentHandle conditions a good app should anticipate and recover fromSignal failures that usually cannot be recovered from
Error handlingForces clean error handlingAllows faster development with less restriction
RobustnessRequire explicit handling to stay robustGive more freedom but risk runtime errors
StabilityHelp create predictable, stable programsCan cause unexpected termination if unhandled
VerbosityMore verbose due to mandatory handlingLess verbose due to optional handling
ResponsibilityEncourage graceful handlingPut the responsibility on the developer
Typical useExpected conditions that can be recovered fromUnexpected or logical errors

Code Examples

Flow diagram where the compiler asks if an exception is checked; if yes it must be caught or declared with throws, if no handling is optional
The compiler forces a checked exception to be caught or declared; unchecked handling is optional.

The clearest way to see the difference is one method of each kind.

A checked exception must be declared or caught. Here readFile can throw IOException, so the method declares it with throws, otherwise the code would not compile.

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class FileHandler {
    public void readFile(String fileName) throws IOException {
        File file = new File(fileName);
        FileReader fr = new FileReader(file);  // may throw IOException
        // read file contents
        fr.close();
    }
}

An unchecked exception needs no declaration. Here divide throws ArithmeticException, which extends RuntimeException, so the compiler does not force any handling.

public class MathUtil {
    public int divide(int dividend, int divisor) {
        if (divisor == 0) {
            throw new ArithmeticException("Cannot divide by zero");
        }
        return dividend / divisor;
    }
}

Step-by-step handling:

  • Create a class or method that may throw exceptions.
  • Identify whether each exception is checked or unchecked.
  • Handle a checked exception by catching it or declaring it with throws.
  • Handle an unchecked exception with a try-catch, or let it propagate up the call stack.

Best practices:

  • Use checked exceptions for recoverable errors, and unchecked for programming errors.
  • Avoid catching unchecked exceptions unless necessary, to keep the code clear.
  • Always provide a meaningful error message when handling an exception.

Common pitfall: swallowing an exception by catching it without any real handling. Solution: always log the exception or rethrow it after handling, so failures never happen silently.

When to Use Which

You choose the type by asking whether the caller can reasonably recover.

Use a checked exception for a recoverable condition that a method should anticipate, such as a missing file or a dropped network connection. Because the compiler enforces handling, the caller cannot forget to deal with it. So checked exceptions suit expected, external failures.

Use an unchecked exception for a programming error that a caller cannot sensibly recover from, such as a null reference or a bad array index. Since handling is optional, the code stays clean, and the bug surfaces during testing. Therefore unchecked exceptions suit logic errors that you should fix, not catch.

Interview Questions

RuntimeException is the dividing line. Any exception that extends RuntimeException is unchecked, while any that extends Exception but not RuntimeException is checked. Both ultimately descend from Throwable, so that one class decides the rule.

Yes. The compiler enforces checked exceptions, so a method that can throw one must catch it or declare it with throws, or the code fails to compile. It does not enforce unchecked exceptions, so handling them is entirely optional. That single rule is the whole difference at compile-time.

Error is unchecked, and it sits on its own branch under Throwable, separate from Exception. It marks serious problems the application usually cannot recover from, such as OutOfMemoryError or StackOverflowError. So you rarely catch an Error on purpose.

Yes, you can catch an unchecked exception with a try-catch just like a checked one, but it is not required. Most of the time a NullPointerException or a bad index means a bug, so fixing the code is better than catching it. Still, catching is allowed when it genuinely helps.

Frequently Asked Questions

Java exceptions are objects that represent an abnormal condition which disrupts the normal flow of a program. When an error or exceptional event occurs, a Java application uses exceptions to handle the situation gracefully. So they let the program react to problems instead of crashing blindly.

Checked exceptions must be either caught at compile-time or declared in the method’s throws clause. Unchecked exceptions, on the other hand, need not be caught or declared, which makes them more flexible but also riskier, since they can lead to runtime errors. So the compiler enforces one and ignores the other.

A checked exception must be either caught with a try-catch block or declared in the method signature using the throws keyword. If you fail to do either, the code will not compile. So the compiler guarantees that every checked exception is at least acknowledged.

Yes, unchecked exceptions can be caught with a try-catch block just like checked exceptions, though it is not mandatory. Unchecked exceptions usually signal programming errors, such as a null pointer or an out-of-bounds index. So fixing the cause is often better than catching the symptom.

Use checked exceptions for recoverable conditions a method can reasonably handle, such as a file not found or a network issue. Use unchecked exceptions where recovery is unlikely or impractical, such as programming errors or critical failures. So the likelihood of recovery guides the choice.

The keyword throw actually raises an exception at a point in the code, so it takes one exception object. The keyword throws appears in a method signature and declares which checked exceptions the method may pass to its caller. So throw triggers an exception, while throws warns about one.

Wrapping Up

Checked and unchecked exceptions split Java error handling by one rule: does the compiler enforce it? Checked exceptions extend Exception, mark recoverable problems, and must be caught or declared. Unchecked exceptions extend RuntimeException, mark programming bugs, and need no handling.

Remember the simple test: if it extends RuntimeException, it is unchecked and optional; otherwise it is checked and enforced. Match checked exceptions to recoverable, external failures and unchecked exceptions to logic errors, and most exam and interview questions on the two fall into place.

Related reading on DiffStudy:

Whatsapp-color Created with Sketch.

By Arun Kumar

Full Stack Developer with a BE in Computer Science, working with React, Next.js, Node.js, MongoDB, and AI/ML tools. Founder of DiffStudy — built to help CS students ace GATE and university exams, and keep developers up to date across AI, cloud, system design, web development, and every field of computer science. Every article is written from real hands-on experience, not just theory.

Leave a Reply

Your email address will not be published. Required fields are marked *


You cannot copy content of this page