10

I am new to Java and was reading its documentation on exceptions., and especially the Unchecked Exceptions — The Controversy page.

The bottom-line says:

If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.

I don't understand the article. What “the controversy” is all about? Can you explain it in simple words?

3

I'll give you an example first (but at the very end is the answer why the controversy).

Let's suposse you are editing a document in a Java-based document editor and after you are done you choose File->Save as... and you chose to save the document into a volume you don't have write permission on. The Editor wouldn't crash on you with an ugly stacktrace, it would simply tell you that it couldn't save the file and it would let you continue editing and/or save to another location.

In such a case it's probably a checked exception was expected, caught and acted upon to graciously recover from it.

On the other hand suposse these a division by zero or a null pointer exception caused by a programming error that rears its ugly head only in certain conditions. That could happen anywhere in the code, the RAM can be corrupted, etc. No API doc would tell you "this method would throw a division by zero if RAM is corrupted".

Checked exceptions should be part of the design and users of that API should prepare to handle them. Unchecked exceptions could happen almost everywhere and are beyond our control.

The controversy arises from programmers using unchecked exceptions (extending from RuntimeException) when they should be using checked exceptions:

  • as a shorcut not to be bothered by the compiler
  • to make their signatures look simpler
  • because they consider that checked exceptions are a dependency issue (if you throw a new checked exception in an implementing class you should modify the interface's signature) and viceversa.
  • "you should modify the interface's signature" - well, you are even forced to do so by the compiler, and you'll have to handle or declare it to be thrown at each call site. – Hulk Jul 20 '16 at 12:41
  • 3
    Whether a UI application properly handles an error in a user-friendly way is based on how well the programmer wrote the code. They can write the code just fine that way with unchecked exceptions, just as someone else can improperly handle the errors with checked exceptions. The intent of checked exceptions is to make it easier for programmers to properly handle errors. The controversy is not what you've claimed here, but rather whether or not they actually successfully accomplish that goal of actually making error handling easier. In the view of many, they do not; they make it harder. – Servy Jul 20 '16 at 15:17
  • @Servy In what user friendly way could a UI app handle the fact of a failing RAM chip, or being left with no CPU cycles because another software malfunctioned? – Tulains Córdova Jul 20 '16 at 15:21
  • 1
    @TulainsCórdova In either of those cases the program isn't even going to run, so there is no exception handling being done period, so how you try to represent an error is irrelevant when you aren't able to ever even run any of your code when it happens. – Servy Jul 20 '16 at 15:25
  • 1
    @TulainsCórdova You don't necessarily have to have encountered the error to handle it, but yes, the compiler won't tell you that it could be thrown. The controversy is whether or not the compiler telling you that the exception could be thrown is actually helpful. Some think it is, some think it isn't. The question here is what is the controversy, and that is the answer. Your statement of what the core controversy of checked exceptions is is not it. – Servy Jul 20 '16 at 15:34
-3

There is no controversy on that page. It's Oracle telling people to use checked exceptions.

The fake 'controversy' that they have invented here is between the language designers and the language users. The designers allowed people to throw and catch things that (in their mind) should not be thrown or caught. So they created a web page complaining about lazy developers.

  • 4
    There is a controversy even if that page says little about it. What's controversial is the answer to the question, "Do checked exceptions help or hinder the development of better software?" And, "If so, which exceptions should be checked, and which should be unchecked?" I'll wager that you could start a lively discussion if you got enough developers (and beer) together in a room to answer those questions. – Solomon Slow Jul 25 '16 at 20:21

protected by gnat Jul 25 '16 at 18:25

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.