Exploring Checked and Unchecked Exceptions in Java Programming

Caner Ünal
Codimis
Published in
3 min readJul 13, 2023
404
Photo by visuals on Unsplash

Exception handling is a fundamental aspect of Java programming, allowing developers to handle unforeseen errors and exceptional situations gracefully. In Java, exceptions are broadly classified into two categories: Checked and unchecked exceptions.

Understanding the distinctions between these two types of exceptions is crucial for writing reliable and maintainable code. This article aims to provide details about checked and unchecked exceptions in Java, their usage scenarios, and appropriate coding examples.

Checked and unchecked exceptions differ in how they are handled during the development process and their implications for the overall codebase.

Checked Exception

Checked exceptions, as the name suggests, are exceptions that the Java compiler enforces to be explicitly handled in the code. They are derived from the Exception class or its subclasses, excluding RuntimeException and its subclasses. The compiler ensures that the developer either includes a try-catch block to handle the checked exception or declares it using the throws clause.

IOException and ParseException are examples of checked exceptions.

Unchecked Exceptions

On the other hand, unchecked exceptions do not require explicit handling at compile-time. They are derived from the RuntimeException class or its subclasses. Unlike checked exceptions, the compiler does not enforce explicit handling of unchecked exceptions, offering developers more flexibility.

NullPointerException and IllegalArgumentException are examples of unchecked exceptions.

Checked Exceptions Usage Scenarios

Checked exceptions are suitable when the exceptional situation is expected and can be reasonably recovered from by the calling code. These exceptions often signify external factors or conditions beyond the code’s control that can lead to errors.

To illustrate, consider the following code snippet:

public void readFile(String fileName) throws IOException {
try (FileInputStream fis = new FileInputStream(fileName)) {
// Perform file reading operations
} catch (IOException e) {
// Handle the exception appropriately
}
}

In this example, the ‘readFile’ method throws a checked exception, IOException, indicating potential issues with file input/output operations. The calling code can either handle the exception within a catch block or propagate it to an upper layer for further handling.

Unchecked Exception Usage Scenarios

Unchecked exceptions are ideal for situations where exceptional conditions arise due to programming errors or unexpected scenarios that cannot be reasonably recovered from. These exceptions typically indicate severe problems that require immediate attention and resolution during the development and testing phases.

Consider the following code snippet:

public int divide(int dividend, int divisor) {
if (divisor == 0) {
throw new ArithmeticException("Divisor cannot be zero");
}
return dividend / divisor;
}

Here, the ‘divide’ method throws an unchecked exception, ArithmeticException, when the divisor is zero. This exception signals a logical flaw in the code, and it is not expected to be explicitly handled. Instead, it serves as a notification to the developer to rectify the code logic or handle the zero divisor scenario before invoking the method.

In conclusion, understanding the differences between checked and unchecked exceptions is vital for writing robust and maintainable Java code. By appropriately selecting the exception type, developers can enhance code readability, improve error handling, and ensure the reliability of their Java applications.

By grasping the concepts discussed in this article, you will be equipped with regarding the usage of right type of exceptions in your Java programs.

--

--