---
title: "Understanding checked vs unchecked exceptions in Java"  
description: "Understanding checked vs unchecked exceptions in Java"  
author: "Revati S Misra"  
published: 2023-07-28  
updated: 2023-07-29  
canonical: https://www.mindstick.com/forum/159359/understanding-checked-vs-unchecked-exceptions-in-java  
category: "java"  
tags: ["exception handling", "java", "exception"]  
reading_time: 3 minutes  

---

# Understanding checked vs unchecked exceptions in Java

[Understanding](https://www.mindstick.com/articles/12918/cat-5e-vs-cat-6a-understanding-the-major-differences) checked vs unchecked [exceptions](https://www.mindstick.com/interview/22871/define-predifined-generic-exceptions) in [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming)

## Replies

### Reply by Aryan Kumar

Sure, I can help you with that.

In Java, there are two types of exceptions: checked exceptions and unchecked exceptions.

- **Checked exceptions** are exceptions that are checked at compile time. This means that if a method throws a checked exception, the method must either handle the exception or it must specify the exception using the `throws` keyword. If the method does not handle or specify the exception, the compiler will not allow the code to compile.
- **Unchecked exceptions** are exceptions that are not checked at compile time. This means that a method does not have to handle or specify an unchecked exception. If an unchecked exception is thrown, the program will simply crash.

Here is a table that summarizes the differences between checked and unchecked exceptions in Java:

| Feature | Checked Exceptions | Unchecked Exceptions |
| --- | --- | --- |
| Checked at compile time | Yes | No |
| Must be handled or specified | Yes | No |
| Crashes the program if not handled | No | Yes |
| Examples | IOException, SQLException, ClassNotFoundException | NullPointerException, ArrayIndexOutOfBoundsException, ArithmeticException |

Which type of exception should you use? The general rule of thumb is to use checked exceptions for exceptions that can be programmatically recovered from, and unchecked exceptions for exceptions that cannot be programmatically recovered from.

For example, if a method opens a file, it should throw a checked exception if the file cannot be opened. This is because the method can be programmatically recovered from by trying to open a different file. However, if a method tries to divide a number by zero, it should throw an unchecked exception. This is because there is no way to programmatically recover from this exception.

Here are some additional guidelines for using checked and unchecked exceptions in Java:

- Use checked exceptions to indicate errors that are outside the control of the programmer. For example, if a method throws a `IOException` because the user's disk is full, this is an error that is outside the control of the programmer.
- Use unchecked exceptions to indicate errors that are the fault of the programmer. For example, if a method throws a `NullPointerException` because the programmer passed a `null` reference to the method, this is an error that is the fault of the programmer.
- Don't overuse checked exceptions. If you use checked exceptions too often, your code will become difficult to read and maintain.


---

Original Source: https://www.mindstick.com/forum/159359/understanding-checked-vs-unchecked-exceptions-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
