Checked and Unchecked Exceptions in Java

1. Overview

Java exceptions fall into two main categories: checked exceptions and unchecked exceptions. In this article, we’ll provide some code samples on how to use them.

2. Checked Exceptions

In general, checked exceptions represent errors outside the control of the program. For example, the constructor of FileInputStream throws FileNotFoundException if the input file does not exist.

Java verifies checked exceptions at compile-time.

Therefore, we should use the throws keyword to declare a checked exception:

private static void checkedExceptionWithThrows() throws FileNotFoundException {
    File file = new File("not_existing_file.txt");
    FileInputStream stream = new FileInputStream(file);
}

We can also use a try-catch block to handle a checked exception:

private static void checkedExceptionWithTryCatch() {
    File file = new File("not_existing_file.txt");
    try {
        FileInputStream stream = new FileInputStream(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

Some common checked exceptions in Java are IOExceptionSQLException, and ParseException.

The Exception class is the superclass of checked exceptions. Therefore, we can create a custom checked exception by extending Exception:

public class IncorrectFileNameException extends Exception {
    public IncorrectFileNameException(String errorMessage) {
        super(errorMessage);
    }
}

3. Unchecked Exceptions

If a program throws an unchecked exception, it reflects some error inside the program logic. For example, if we divide a number by 0, Java will throw ArithmeticException:

private static void divideByZero() {
    int numerator = 1;
    int denominator = 0;
    int result = numerator / denominator;
}

Java does not verify unchecked exceptions at compile-time. Furtheremore, we don’t have to declare unchecked exceptions in a method with the throws keyword. And although the above code does not have any errors during compile-time, it will throw ArithmeticException at runtime.

Some common unchecked exceptions in Java are NullPointerExceptionArrayIndexOutOfBoundsException, and IllegalArgumentException.

The RuntimeException class is the superclass of all unchecked exceptions. Therefore, we can create a custom unchecked exception by extending RuntimeException:

public class NullOrEmptyException extends RuntimeException {
    public NullOrEmptyException(String errorMessage) {
        super(errorMessage);
    }
}

4. When to Use Checked Exceptions and Unchecked Exceptions

It’s a good practice to use exceptions in Java so that we can separate error-handling code from regular code. However, we need to decide which type of exception to throw. The Oracle Java Documentation provides guidance on when to use checked exceptions and unchecked exceptions:

“If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.”

For example, before we open a file, we can first validate the input file name. If the user input file name is invalid, we can throw a custom checked exception:

if (!isCorrectFileName(fileName)) {
    throw new IncorrectFileNameException("Incorrect filename : " + fileName );
}

In this way, we can recover the system by accepting another user input file name. However, if the input file name is a null pointer or it is an empty string, it means that we have some errors in the code. In this case, we should throw an unchecked exception:

if (fileName == null || fileName.isEmpty())  {
    throw new NullOrEmptyException("The filename is null or empty.");
}

5. Conclusion

In this article, we discussed the difference between checked and unchecked exceptions. We also provided some code examples to show when to use checked or unchecked exceptions.

As always, all code found in this article can be found over on GitHub.

Related posts:

Java Program to Implement Red Black Tree
Introduction to Netflix Archaius with Spring Cloud
Rate Limiting in Spring Cloud Netflix Zuul
Java Program to Perform Finite State Automaton based Search
Spring Boot with Multiple SQL Import Files
Java Program to Compute DFT Coefficients Directly
Java Program to Solve the 0-1 Knapsack Problem
Java Program to Implement Pairing Heap
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
Java Program to Represent Graph Using Incidence Matrix
HttpClient Connection Management
Sao chép các phần tử của một mảng sang mảng khác như thế nào?
Java Program to Implement Adjacency List
The Guide to RestTemplate
Converting Between Byte Arrays and Hexadecimal Strings in Java
Java Byte Array to InputStream
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
Lập trình mạng với java
Convert Hex to ASCII in Java
Registration – Activate a New Account by Email
Java Program to Implement Cartesian Tree
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
Câu lệnh điều khiển vòng lặp trong Java (break, continue)
Java Program to Implement Floyd Cycle Algorithm
Default Password Encoder in Spring Security 5
Spring Boot - Exception Handling
Java – Reader to Byte Array
HashSet trong Java hoạt động như thế nào?
Recommended Package Structure of a Spring Boot Project
Java Program to Implement Binomial Tree
Java Collections Interview Questions
Java Program to Implement HashTable API