A
A
Alexey Nikolaev2018-02-02 18:46:22
Programming
Alexey Nikolaev, 2018-02-02 18:46:22

What is the best way to apply exceptions?

Good evening.
Usually, when it comes to exceptions, two approaches prevail in the discussion. The first is to apply exceptions to handle any errors. I even read in "Clean Code" that it would be nice to throw out return null and replace them with exceptions in order not to check for null, but to process exceptions. Some use exceptions even when there are no errors, but there is a certain situation - for example, there is no money in the account, we throw an exception; the object does not have the required field or the nonce is incorrect - we throw an exception.
And the second is to apply exceptions only for some exceptional situations, i.e. when the correct operation of the program after such a situation is not possible. And never use exceptions unless you need to do something meaningful. The rest of the errors are handled by regular means.
I rarely use exceptions, mostly when working with a database or with third-party APIs. But I understand why the first option is alive - exceptions are very convenient. Which approach is better and more correct?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Demian Smith, 2018-02-02
@Heian

The first option is preferable. Because the first option is a recipe: always throw an exception when a function cannot return (or do) what is expected of it.
The second option is unable to provide an unambiguous solution, because "what is a program-level exception" can be argued endlessly.

M
Mercury13, 2018-02-02
@Mercury13

Declared Java exceptions are bad. However, these declared exceptions make it clear that there are three types of accidents.
1. The execution environment feels bad, the program execution is under threat (the self-check has worked in some part of the program that is not under the control of the programmer). For example, the operation of some built-in detector that determines the entry into someone else's memory. Fall out anywhere. In practice, they do not have to be thrown away or caught.
2. The logic of the program is violated (a self-test that depends on the programmer has worked). Invalid argument, array out of bounds. Fall out anywhere. They are often discarded and rarely caught.
3. An unstable part of the software environment feels bad. File not found, no link. They drop out at certain points. Often thrown away and often caught. In Java, they only need to be declared.
So your holivar relates to the question: should I throw an exception if a Type 3 crash occurs? I personally noticed such questions only in one language - C++. I think it has to do with two things. 1) High overhead associated with certain exception handling techniques. 2) Quite indistinct behavior of C++ when an exception is thrown from a constructor.
If we move away from the lack of a specific language - even about the usual HTTP 404 there is an "Indy path" and a "cURL path" - throw an exception and report the document not found by other methods. For the first and second, you can find a bunch of arguments.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question