---
title: "Explain the difference between checked and unchecked exceptions. Provide examples."  
description: "Explain the difference between checked and unchecked exceptions. Provide examples."  
author: "Anubhav Sharma"  
published: 2024-07-18  
updated: 2024-07-18  
canonical: https://www.mindstick.com/forum/160948/explain-the-difference-between-checked-and-unchecked-exceptions-provide-examples  
category: "java"  
tags: ["java", "javac"]  
reading_time: 3 minutes  

---

# Explain the difference between checked and unchecked exceptions. Provide examples.

[Explain the difference](https://www.mindstick.com/forum/156125/can-you-explain-the-difference-between-organic-and-paid-results) between [checked and unchecked exceptions](https://www.mindstick.com/forum/159233/explain-the-difference-between-checked-and-unchecked-exceptions-in-java). Provide examples.

## Replies

### Reply by Ravi Vishwakarma

In Java, exceptions are categorized into two main types: checked exceptions and unchecked exceptions. Understanding their differences is crucial for handling errors effectively in your code.

#### Checked Exceptions

Checked exceptions are the exceptions that are checked at compile-time. This means the compiler checks that these exceptions are caught or declared to be thrown in the method signature using `throws` keyword.

**Usage:** Typically used to indicate conditions that a well-behaved application should anticipate and handle. Examples include I/O errors, database connection issues, and file not found situations.

## Examples:

- `IOException`
- `SQLException`
- `FileNotFoundException`

## Example Code:

```java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ReadFileExample {
    public static void main(String[] args) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("example.txt"));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
```

In this example, `IOException` is a checked exception that occurs when handling file operations like reading from a file. It must be caught or declared in the method signature using `throws`.

#### Unchecked Exceptions (Runtime Exceptions)

Unchecked exceptions are exceptions that are not checked at compile-time. They occur at runtime and do not need to be explicitly handled or declared using `throws`.

**Usage:** Typically caused by programming bugs, such as accessing an array out of bounds or attempting to perform arithmetic division by zero. These are usually unexpected conditions and may indicate logic errors in the code.

## Examples:

- `NullPointerException`
- `ArrayIndexOutOfBoundsException`
- `ArithmeticException`

## Example Code:

```java
public class DivideByZeroExample {
    public static void main(String[] args) {
        int dividend = 10;
        int divisor = 0;
        try {
            int result = dividend / divisor; // ArithmeticException occurs here
            System.out.println("Result: " + result);
        } catch (ArithmeticException e) {
            System.err.println("Error: Division by zero");
        }
    }
}
```

In this example, `ArithmeticException` is an unchecked exception that occurs when attempting to divide by zero (`divisor = 0`). It does not need to be explicitly handled with `throws`.

## Key Differences

| Feature | Checked Exceptions | Unchecked Exceptions |
| --- | --- | --- |
| **Checked at Compile-Time** | Yes | No |
| **Handling Requirement** | Must be caught or declared (using `throws`) | Optional; can be caught or ignored |
| **Examples** | `IOException`, `SQLException` | `NullPointerException`, `ArrayIndexOutOfBoundsException` |
| **Typical Use Cases** | I/O operations, database access, file handling | Programming errors, unexpected conditions |

## Summary

- **Checked Exceptions:** Checked at compile-time, must be caught or declared using `throws`, used for recoverable conditions.
- **Unchecked Exceptions:** Not checked at compile-time, optional to handle, used for programming errors and unexpected conditions.

Understanding these distinctions helps in designing robust error-handling mechanisms in Java applications, ensuring proper handling of both anticipated and unforeseen exceptions.

## Read more

[**What is the purpose of the volatile keyword in Java?**](https://www.mindstick.com/forum/160950/what-is-the-purpose-of-the-volatile-keyword-in-java)

[**What are annotations in Java? How are they used?**](https://www.mindstick.com/forum/160949/what-are-annotations-in-java-how-are-they-used)

[**Describe the life cycle of a thread in Java.**](https://www.mindstick.com/forum/160947/describe-the-life-cycle-of-a-thread-in-java)


---

Original Source: https://www.mindstick.com/forum/160948/explain-the-difference-between-checked-and-unchecked-exceptions-provide-examples

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
