Java – is it a good idea to use checked exceptions in external APIs?

It is not uncommon to view checked predictions in the API, and one of the most famous examples is closed IOException. In close() And it really bothers me to deal with this exception often A more annoying example is one of our projects It consists of several components, each declaring a specific checked exception The problem (in my opinion) is that when designing, you don't know exactly what some exceptions are So, for example, the component configurator declares configuratorexception When I asked why we didn't use unchecked exceptions, I was told that we wanted our application to be robust, not at runtime But it approaches a weak argument because:

>Most of these exceptions effectively make apps unusable Yes, it didn't explode, but it couldn't make the message record in the flood log. > These exceptions are not specific, but actually mean that "something bad has happened" How should customers recover? > In fact, all recoveries involve recording an exception and swallowing it This is executed in a large try catch statement

I think this is a recurring pattern But still checked exceptions are widely used in the API What's the reason? Are there some types of APIs that are better suited for checking exceptions?

Solution

There are many converses around this problem

Take a look at this topic http://www.mindview.net/Etc/Discussions/CheckedExceptions This classic article

I personally tend to support my own use of runtime exceptions and begin to consider using checked exceptions in the API

In fact, some very popular Java APIs have begun to do the same thing. Hibernate has abandoned the use of checked exceptions of runtime from version 3, and the spring framework tends to use runtime instead of checking exceptions

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>