---
title: "Differentiate between checked and unchecked exceptions in Java with examples."  
description: "Exceptions are categorized into two main types that are checked exceptions and unchecked exceptions."  
author: "Ashutosh Patel"  
published: 2024-07-23  
updated: 2024-07-23  
canonical: https://www.mindstick.com/blog/304521/differentiate-between-checked-and-unchecked-exceptions-in-java-with-examples  
category: "java"  
tags: ["exception handling", "java", "exception"]  
reading_time: 3 minutes  

---

# Differentiate between checked and unchecked exceptions in Java with examples.

#### Checked and Unchecked Exception in Java

Extracts are divided into two main types: standard and unmarked extracts. The difference between these types mainly affects how they are handled by the compiler and the runtime environment.

#### Checked Exceptions

Checked exceptions are exceptions that are checked at compile-time. This means that the compiler ensures that these exceptions are declared caught or thrown by a possible method. If a method throws a tried exception, the caller must either [handle the exception](https://www.mindstick.com/forum/159349/who-and-how-handle-the-exception-when-we-use-throws) (using try-catch) or propagate it to the calling stack (by declaring it in a method signature that throws).

## Examples of Checked Exceptions

\
**IOException-** This exception is thrown when there is a problem [reading or writing](https://www.mindstick.com/forum/158632/explain-how-to-handle-an-ioexception-when-reading-or-writing-files-in-an-asp-dot-net-mvc-project) from a file or network.

## Example-

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

\
In this example, the `FileReader` and `readLine()` methods can throw an `IOException`, which is an observed exception.

**SQLException-** This exception is thrown when an error occurs in the database.

## Exception

```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Example {
   public static void main(String[] args) {
       Connection conn = null;
       Statement stmt = null;
       try {
           conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "username", "password");
           stmt = conn.createStatement();
           // execute SQL queries
       } catch (SQLException e) {
           e.printStackTrace();
       } finally {
           try {
               if (stmt != null) stmt.close();
               if (conn != null) conn.close();
           } catch (SQLException e) {
               e.printStackTrace();
           }
       }
   }
}
```

\
Here the `getConnection()` method can throw a SQLException.

#### Unchecked Exceptions (Runtime Exceptions)

Unchecked objects, also called [runtime exceptions](https://www.mindstick.com/forum/159442/handle-runtime-exceptions-in-mern), need not be declared in a method's `throws` clause, and are not handled by the compiler These exceptions are often caused by programming errors, such as an array going out of bounds, call a method on a `null` [object reference](https://www.mindstick.com/forum/376/system-nullreferenceexception-object-reference-not-set-to-an-instance-of-an-object), or [divide by zero](https://www.mindstick.com/forum/158622/describe-the-concept-of-a-divide-by-zero-exception-and-how-to-handle-it)

## Examples of Unchecked Exceptions

**NullPointerException-** This [exception occurs](https://www.mindstick.com/forum/159341/how-to-use-try-and-catch-in-java-for-exception-handling-and-what-happens-when-an-exception-occurs) when you try to call a method or access a variable on a `null` object reference.

```java
public class Example {
   public static void main(String[] args) {
       String str = null;
       System.out.println(str.length()); // This will throw NullPointerException
   }
}
```

\
**ArrayIndexOutOfBoundsException-** This exception occurs when you try to access an array element with an incorrect index.

```java
public class Example {
   public static void main(String[] args) {
       int[] arr = new int[5];
       System.out.println(arr[10]); // This will throw ArrayIndexOutOfBoundsException
   }
}
```

#### Key Differences

## Handling Requirement

- Observed exceptions must be caught with the try-catch block or reported as thrown with the `throws` keyword in the method signature.
- [Unhandled exceptions](https://www.mindstick.com/forum/159534/how-to-remove-unhandled-exceptions-in-c-plus-plus) need not be caught explicitly or explicitly.

## Examples

- The exceptions reviewed typically involve external resources such as IO processing, [network connectivity](https://answers.mindstick.com/qa/99796/what-are-some-common-steps-to-take-when-troubleshooting-network-connectivity-issues), or database issues.
- Unchecked exceptions often indicate systematic errors or unexpected circumstances.

## Propagation

- Checked exceptions expand the call stack until they are caught.
- Unchecked exceptions expand the call stack, but need not be expanded in method signatures.

Understanding these differences helps establish robust **[exception handling](https://www.mindstick.com/articles/12240/introduction-of-exception-handling)** procedures in Java applications, ensuring that expected errors are handled properly, and properly resolving unexpected events.

**Also, Read:** [Factory vs Abstract Factory Design Patterns in Java](https://www.mindstick.com/articles/336478/factory-vs-abstract-factory-design-patterns-in-java)

---

Original Source: https://www.mindstick.com/blog/304521/differentiate-between-checked-and-unchecked-exceptions-in-java-with-examples

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
