The question is inspired by this question. There, everyone advises me to convert the checked exception to an uncheckable one.

And my question is this: why did the Java developers come up with such a term as a проверяемое исключение ? What is the profit from this? To know which method can throw an exception, and which not? So any method can throw out any NullPointerException that you still need to catch and handle? So how does a NullPointerException differ from an IOException which must be declared in throws ? There is no such term in any language known to me.

The only explanation I can think of is that when a checked exception occurs, the object is in a certain stipulated state, and if unchecked, the object can be damaged.

  • four
    Unchecked exception is a logical error that was not foreseen by the subject area . Therefore, this type of exceptions, unlike those that are checked, can be ignored by the application and raised to the system handler, which crashes the program. If such errors are part of the subject area, then they can be specified in throws . - ߊߚߤߘ
  • @Arhad but this instruction does not oblige anyone to anything - Anton Shchyrov

3 answers 3

The question of checked and unchecked exceptions is quite controversial and often arises, especially when a bunch of new APIs appear that they prevent them from developing normally (the same streams and lambdas for example). Therefore, it is not entirely correct to argue about the correctness of their input more than 20 years after the appearance of the language.

NPE is unverifiable simply because it can theoretically occur in any line. Division by zero potentially covers all cases of division of integers, and OutOfMemoryError can OutOfMemoryError during the creation of any object. Similar reasons can be found for the remaining exceptions.

  • I am not asking why NPE is unverifiable. I’m wondering why we need verifiable ones if, as you rightly noted, an exception can occur in any line of the program - Anton Shchyrov
  • @AntonShchyrov. That there was a "controlled" exception, which the program is obliged to process and the stability of the program does not break. For example, if the user selects a non-existent file, it is necessary that the application does not crash, but stably process the type of error and notify the user. - Anton Mukhin
  • @ Anton Mukhin Well, and what prevents to realize this through unchecked exceptions? Maybe I have a global exception handler somewhere 10 calls back on the stack. Am I obliged to write throws in all 10 methods? Well, stupid is the same - Anton Shchyrov
  • @Zefick And why were they invented at all? And yes, I do not think it is wrong to argue. Because Nothing prevents developers in any Java 10 to abandon this mechanism. Compatibility is not broken, and the crutches in the code will be less - Anton Shchyrov

NullPointerException will never happen if you do a null check. IOException - such an exception may voniknut, even if you try to determine in advance whether it will occur. In essence, this is for the programmer as “clean code”. It makes the architecture more convenient and beautiful.
Those. if, for example, you see that you can not check the error, make it runtime. Somehow, I think.

  • Checked exceptions, or rather the requirements of their interception, pollute the code. I gave an example in a related question - Anton Shchyrov
  • @AntonShchyrov Yes, so there is a good practice in some parts of the code to convert the checked exception to its own, not checked. - Anton Mukhin
  • This “good practice” looks like a workout of a silly implementation - Anton Shchyrov

Using checks in the code, you can ensure that there is never an NPE, but the presence of a file on the hard disk is not guaranteed, it can be deleted, so you need to handle such situations in advance