Which option of applying exceptions better?

Good evening.
Usually, when talking about exceptions, in the discussion, two approaches prevail. The first is to use exceptions to handle any errors. Read even in "Clean code" that it would be nice to throw out return null and replace them with exceptions, not to null check and exception processing. Some use an exception even when the error really, but there is a certain situation - for example, there is no money in the account, throw exception; object has no required fields or invalid nonce - throw the exception.

And the second is to use exceptions only for certain exceptional situations, i.e. when the correct operation of the program after such a situation is not possible. And never use the exception if you do not want to do something meaningful. Other error processing regular means.

I very rarely use exceptions, mostly when working with the database or with third-party API. But I understand why alive the first option the exceptions are very comfortable. Which approach is better and correct?
Thanks in advance.
June 8th 19 at 16:38
3 answers
June 8th 19 at 16:40
The first option is preferable. Because the first option is a recipe: always throw an exception when a function can't return (or do) what is expected of her.

The second option is unable to provide a definite decision, because of "the extreme situation at the programme level" it is possible to argue infinitely.
June 8th 19 at 16:42
June 8th 19 at 16:44
The declared Java exception is bad. However, this declared exception, allow you to realize that there are three types of accidents.

1. The runtime feels bad, the execution of the program under the threat (the self test is triggered in some part of the program, not controlled by the programmer). For example, operation of a built-in detector that identifies an entry in someone else's memory. Fall out anywhere. In practice it is not necessary neither to throw nor catch.

2. Broken program logic (triggered self-test, depending on the programmer). Invalid argument, the boundary of the array. Fall out anywhere. They are often discarded and rarely get caught.

3. The unstable part of the software environment feels bad. File not found, no connection. Drop out at certain points. Often thrown away and often catch. In Java only need to declare them.

Thus, your holivar relates to the question: whether to throw an exception if the accident happened, the 3rd type? I have personally noticed similar issues only in a single language — C++. I think this is due to two things. 1) higher costs associated with certain methods of exception handling. 2) is Quite vague behavior of C++ when loss exception from the constructor.

If you move away from the lack of concrete language — even on the standard HTTP 404 "the way Indy" and "cURL path" is to throw an exception and report the missing document by other methods. For the first and for the second you can find a lot of arguments.

Find more questions by tags ExceptionsProgramming