Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why one should try throw unchecked exception over checked exception? [duplicate]

I've been told that I should consider throwing Unchecked exception over Checked exception in my code and not only that, but to extend the RuntimeException with my own one. Now, I do understand the difference between the two, but still doesn't understand why should I do that?

If I have this method header which throws 2 kinds of exceptions:

public static Optional<String> getFileMd5(String filePath) throws NoSuchAlgorithmException, IOException {}

Why should I replace them with one (less detailed) exception?

like image 701
Nimrod Avatar asked Jun 15 '16 11:06

Nimrod


People also ask

Should you throw checked or unchecked exceptions?

Checked Exceptions should be used for predictable, but unpreventable errors that are reasonable to recover from. Unchecked Exceptions should be used for everything else.

Why throws is not used for unchecked exception?

You can use "throws" to declare unchecked exceptions also. But unchecked exceptions are programmer headaches. So throws usually used to handle checked exceptions only as they are anticipated by the compiler whenever you are making use of certain classes and interfaces.

What happens when unchecked exception is thrown?

If a program throws an unchecked exception, it reflects some error inside the program logic. Java does not verify unchecked exceptions at compile-time. Furthermore, we don't have to declare unchecked exceptions in a method with the throws keyword.

Should we throw unchecked exception in Java?

Since unchecked exceptions indicate programming errors, declaring them in the throws clause should be avoided. Generally, catching these exceptions should not be attempted, except for the highest level of your program.


1 Answers

The IOException is acceptable. The caller can't be sure that the filePath exists and will still exist when the method is executed, and your method must be able to signal the problem. IOException is the conventional exception to throw in that case, although you could wrap it inside an UncheckedIOException if you prefer runtime exceptions. An unchecked IO exception would be as clear as a checked IOException. What you would lose (or gain, depending on the point of view) is that you wouldn't force the caller to deal with it.

The NoSuchAlgorithmException exception, on the other hand, should definitely be wrapped into an runtime exception. The caller has no way to do anything if your method uses an algorithm that doesn't exist. It's clearly a bug if that exception happens, and bugs should be signalled by runtime exceptions. So, write your own runtime exception, that wraps the original NoSuchAlgorithmException (so that you don't lose the root cause of the problem if you ever throw it), and don't bother all the callers of your code with an exception that should never, ever happen.

Regarding runtime vs. checked exceptions, it's mainly an opinion-based question, but a few things should be noted though:

  • AFAIK, no new Java API uses checked exceptions anymore. See java.time for example, which never throws checked exceptions although the old equivalent (like DateFormat) throws checked exceptions
  • Java is the only language to have checked exceptions.
  • Checked exceptions don't fit well with functional idioms (lambda expressions, streams, etc.) introduced in Java 8: No functional interface throws a checked exception.

I think it's safe to now say that checked exceptions were an interesting idea, but which proved to be a bad one. You should prefer unchecekd exceptions.

Now, if your question is: how to throw unchecked exceptions rather than checked exceptions, it's quite simple:

public static Optional<String> getFileMd5(String filePath) {
    try {
        // your original code
    }
    catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    catch (NoSuchAlgorithmException e) {
        throw MyCustomCryptoException(e);
    }
}
like image 98
JB Nizet Avatar answered Oct 29 '22 16:10

JB Nizet