Logo

dev-resources.site

for different kinds of informations.

Effective Java: Avoid Unnecessary Use of Checked Exceptions

Published at
8/17/2021
Categories
java
effective
exceptions
architecture
Author
kylec32
Author
7 person written this
kylec32
open
Effective Java: Avoid Unnecessary Use of Checked Exceptions

Many developers are not fans of checked exceptions. Effective Java argues that this doesn't need to be the case. The defining feature of a checked exception is forcing the caller to handle the exception either by catching it or by passing the exception up the call stack via its signature.

Why does being forced to handle checked exceptions bother developers though? I would suggest that it is because they don't know how to handle the exception. Because of this, the compiler forces you to handle it puts the developer in a tough position where no choice feels correct. This is where the difference lies I believe. If, more often than not, the developer that is presented with a checked exception has a clear way of handling it I think this is a good use of the checked exception. It forces the developer to do something that they already are wanting to do. If, however, there is not a clear way forward a checked exception is not the correct way to go. This does entail a tricky line to walk, how can you know at API development time what exceptions will have reasonable ways of handling them at API use time? It is for this reason that I would error on the side of runtime exceptions unless you are absolutely sure.

Other mechanisms that can facilitate some of the desired benefits of checked exceptions. One of these is providing a function that can check whether an operation is allowed that can be called before the operation is called. This may not work all the time but can be a tool that can be useful.

Another technique is to return an Optional and return an empty optional when the data can't be processed. This again can work but we lose the ability to have typed exceptions with state and functions that we get with exceptions in Java. It could also be argued that if this technique makes sense in your case you likely don't have an exceptional case happening and thus you shouldn't have been using exceptions anyway.

Checked exceptions should be used sparingly. This is because they often can make an API painful to use. If a caller doesn't have a reasonable way to handle a checked exception then all they can do is ignore it or pass on the problem to the next caller which leads to more complicated code with no benefit.

effective Article's
30 articles in total
Favicon
What is The Best Time to Learn New Things Effectively?
Favicon
Designing an Efficient Biomass Generator Set System
Favicon
Top 10 Essential Features for an Effective Knowledge Base
Favicon
Effective Java: Consider Serialization Proxies Instead of Serialized Instances
Favicon
Effective Java: For Instance Control, Prefer Enum types to readResolve
Favicon
Effective Java: Write readObject Methods Defensively
Favicon
Effective Java: Consider Using a Custom Serialized Form
Favicon
Effective Java: Implement Serializable With Great Caution
Favicon
Effective Java: Prefer Alternatives To Java Serialization
Favicon
Effective Java: Don't Depend on the Thread Scheduler
Favicon
Effective Java: Use Lazy Initialization Judiciously
Favicon
Effective Java: Document Thread Safety
Favicon
Effective Java: Prefer Concurrency Utilities Over wait and notify
Favicon
Effective Java: Prefer Executors, Tasks, and Streams to Threads
Favicon
Effective Java: Avoid Excessive Synchronization
Favicon
Effective Java: Synchronize Access to Shared Mutable Data
Favicon
Effective Java: Don't Ignore Exceptions
Favicon
Effective Java: Strive for Failure Atomicity
Favicon
Effective Java: Include Failure-Capture Information in Detail Messages
Favicon
Effective Java: Document All Exceptions Thrown By Each Method
Favicon
Effective Java: Throw Exceptions Appropriate To The Abstraction
Favicon
Effective Java: Favor The Use of Standard Exceptions
Favicon
Effective Java: Avoid Unnecessary Use of Checked Exceptions
Favicon
Effective Java: Use Checked Exceptions for Recoverable Conditions
Favicon
Effective Java: Use Exceptions for Only Exceptional Circumstances
Favicon
Effective Java: Adhere to Generally Accepted Naming Conventions
Favicon
Effective Java: Optimize Judiciously
Favicon
Effective Java: Use Native Methods Judiciously
Favicon
Effective Java: Prefer Interfaces To Reflection
Favicon
10 effective tips for New Developers

Featured ones: