Is Checked Exception a Design Mistake in Java?

Pradeesh Kumar
2 min readFeb 6, 2023

--

Photo by Motoki Tonn on Unsplash

Java boasts a robust error-handling system through the use of try-catch-finally blocks, exceptions, throws, etc. Exceptions are categorized into two categories: checked and unchecked. The checked exceptions have always been a controversial feature of the Java language.

Why Checked Exception?

Java compiler forces the programmer to either handle or declare as thrown checked exception in order to enforce good error-handling practices in Java programs and ensure that exceptions are not ignored.

Advantages

  • Error handling: They enforce error handling. Makes the exception a visible part of a method signature.
  • Documentation: Checked exceptions in method signatures aid code documentation and understanding.
  • Design considerations: Forces the developer to consider error handling when designing the code, leading to better error-handling practices and more robust software.

The Criticism

There are many controversies about the checked exceptions in java. It has been criticized for several reasons:

  • Boilerplate code: Handling checked exceptions can result in a lot of boilerplate code, especially in larger projects, leading to increased verbosity and decreased readability.
  • Forced handling: The requirement to either handle or declare checked exceptions can be seen as burdensome, as it forces the programmer to handle exceptions that may not be relevant to their code
  • API design: The requirement to declare checked exceptions in method signatures can impact API design, making it harder to evolve APIs over time and making it more difficult to write methods that throw multiple different exceptions.

(Lesser) Known Facts about Checked Exceptions

  • There is no such thing as a ‘checked exception’: The JVM doesn’t know if it’s a checked exception or unchecked, only the Java language does
  • JVM Languages, such as Groovy, Scala, and Kotlin, have all rejected the idea of checked exceptions.
  • Even Java 8 no longer embrace them in the new Streams API because they are incompatible with the functional style of coding.
  • Popular frameworks such as Spring and Hibernate abandoned them. Also, modern Java frameworks don’t use them.

Conclusion

Most Java developers prefer unchecked exceptions, which do not require explicit handling and can make the code more concise and readable. However, the decision to use checked or unchecked exceptions is largely a matter of personal preference and project requirements.

--

--

Pradeesh Kumar
Pradeesh Kumar

Written by Pradeesh Kumar

Distributed Systems | Cloud Computing | Databases | Software Engineering

Responses (3)