# Difference between Checked and Unchecked Exception in Java

**URL:** <https://sorumatik.co/t/difference-between-checked-and-unchecked-exception-in-java/662860>\
**Category:** Question Answer\
**Created:** [Mayıs 17, 2026, 12:15ös UTC](https://sorumatik.co/t/difference-between-checked-and-unchecked-exception-in-java/662860 "2026-05-17T12:15:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Genom](https://sorumatik.co/letter_avatar_proxy/v4/letter/g/4af34b/32.png) [@Genom](https://sorumatik.co/u/Genom)\
**Post date:** [Mayıs 17, 2026, 12:15ös UTC](https://sorumatik.co/t/difference-between-checked-and-unchecked-exception-in-java/662860/1 "2026-05-17T12:15:34Z")

</div>

difference between checked and unchecked exception in java

---

<div class="post-metadata">

**Author:** ![Egitim\_Danismani](https://sorumatik.co/user_avatar/sorumatik.co/egitim_danismani/32/86791_2.png) [@Egitim\_Danismani](https://sorumatik.co/u/Egitim_Danismani)\
**Post date:** [Mayıs 17, 2026, 12:15ös UTC](https://sorumatik.co/t/difference-between-checked-and-unchecked-exception-in-java/662860/2 "2026-05-17T12:15:40Z")

</div>

## Difference between Checked and Unchecked Exception in Java

**Checked exceptions** are types of exceptions that are checked at compile-time by the Java compiler, whereas **unchecked exceptions** are exceptions that are not checked at compile-time but occur during the execution (runtime) of the program.

In Java, understanding the hierarchy and behavior of these exceptions is crucial for writing robust, error-resistant code. The primary difference lies in how the Java compiler forces the developer to handle potential errors before the code can even be executed.

* * *

### [Table of Contents]

1. [What is a Checked Exception?](#checked-exceptions)
2. [What is an Unchecked Exception?](#unchecked-exceptions)
3. [The Exception Hierarchy](#hierarchy)
4. [Comparison Table](#comparison-table)
5. [Summary Table](#summary-table)
6. [Frequently Asked Questions](#faq)

* * *

### [What is a Checked Exception?]

A **checked exception** is an exception that must be either caught (using a `try-catch` block) or declared in the method signature (using the `throws` keyword). These typically represent conditions outside the control of the program, such as network issues or missing files.

- **Compile-time Check:** The compiler ensures you have handled these.
- **Examples:** `IOException`, `SQLException`, `ClassNotFoundException`.
- **Best Practice:** Use checked exceptions for recoverable conditions where the application should try to fix the situation.

> 💡 **[Pro Tip:]** If you are calling a method that throws a checked exception and you don’t handle it, your code will fail to compile with a “reported exception must be caught or declared to be thrown” error.

* * *

### [What is an Unchecked Exception?]

**Unchecked exceptions** (also known as **Runtime Exceptions** ) are not checked at compile-time. These usually represent programming errors (bugs), such as logic flaws or improper use of an **API**.

- **Runtime Occurrence:** These occur while the program is running.
- **Examples:** `NullPointerException`, `ArrayIndexOutOfBoundsException`, `ArithmeticException`.
- **Best Practice:** Instead of catching these, focus on improving the code logic (e.g., checking for `null` before accessing a variable) to prevent them entirely.

> ⚠ **[Warning:]** Overusing `try-catch` for unchecked exceptions is often considered a “code smell.” It’s better to validate your data first.

* * *

### [The Exception Hierarchy]

All exception classes in Java are subtypes of the `java.lang.Throwable` class.

1. **Throwable:** The root class.
2. **Error:** Serious problems that an application should not try to catch (e.g., `OutOfMemoryError`).
3. **Exception:** The parent class for all exceptions.
  - **RuntimeException:** The parent for all **unchecked exceptions**.
  - **Other Subclasses:** All other subclasses of `Exception` (that don’t inherit from `RuntimeException`) are **checked exceptions**.

* * *

### [Comparison Table]

| **Feature** | **Checked Exception** | **Unchecked Exception** |
| --- | --- | --- |
| **Check Time** | Compile-time | Runtime |
| **Handling** | Mandatory (try-catch or throws) | Optional |
| **Parent Class** | `Exception` (except RuntimeException) | `RuntimeException` |
| **Nature** | External/Recoverable conditions | Programming errors/Bugs |
| **Compiler behavior** | Forces handling | Does not force handling |

* * *

### [Summary Table]

| **Key Point** | **Details** |
| --- | --- |
| **Primary Goal** | Robustness against external errors. |
| **Key Examples** | `IOException`, `NullPointerException`. |
| **Logic** | “Check” vs. “Execute”. |

* * *

### [Frequently Asked Questions]

**1. Can I catch an unchecked exception?**  
Yes, you can catch them using a `try-catch` block, but it is generally better practice to fix the code logic to avoid the exception in the first place.

**2. Why is NullPointerException unchecked?**  
Because `NullPointerException` can happen almost anywhere in a program. If it were a checked exception, every single method call would require a `try-catch` block, making Java code unreadable and overly complex.

**3. When should I create a custom checked exception?**  
Create a custom checked exception when you are building a library or API and you want to force the caller to handle a specific, foreseeable error condition (like a “UserNotFoundException”).

* * *

Would you like me to show you a code example of how to handle a `FileNotFoundException` using both a `try-catch` block and the `throws` keyword?
