Is using checked exceptions in external APIs a good idea?

It is not rare to see a verified version of the API; one of the most famous examples is IOExceptionc Closeable.close(). And often handling this exception really annoys me. An even more annoying example was in one of our projects. It consists of several components, and each component declares a specific checked exception. The problem (in my opinion) is that during development it was not known exactly what the exceptions would be. So, for example, the component is Configuratordeclared ConfiguratorExeption. When I asked why it is not easy to use excepted exceptions, they told me that we want our application to be reliable and not explode at runtime. But this seems like a weak argument because:

  • Most of these exceptions effectively render the application unusable. Yes, it does not explode, but it can do nothing but a flood log with messages.
  • These exceptions are not specific and actually mean that "something bad happened." How should a customer recover?
  • Virtually all recovery comes from log exclusion and then swallowing. This is done in large terms try-catch.

I think this is a repeating picture. Nevertheless, checked exceptions are widely used in the API. What is the reason for this? Are there certain types of APIs that are more suitable for checked exceptions?

+3
source share
5 answers

.

http://www.mindview.net/Etc/Discussions/CheckedExceptions

Runtime API.

, Java API , Hibernate Runtime 3, Spring Framework Runtime .

+2

, 1) , , 2) . - , API - , RuntimeException.

+2

, , , , RuntimeException , ".

, , , , .

3 , , - .

+2

, :

  • a , , " / " ;
  • unchecked exception , " " , ( , , );
  • ru , , , , .

, , , - ( , ). Closable throw IOException, , .

, Java API , . , API- XML , ( XML-, , ...?), API- ( , , , , ...). .

, , " ", , .

, , , " , , , ", " " " RuntimeException Error...

+1

, (, ConfiguratorException) , (, FileNotFound).

In general, I would caution against this, since this is most likely to be a fuzzy abstraction (no one should care about whether your configurator is trying to pull its data from the file system, database, via the network or something else)

If you use checked exceptions, then at least you will find out where and why your abstractions occur. IMHO, this is good.

0
source

Source: https://habr.com/ru/post/1757031/


All Articles