I
I
Inyardic2015-10-28 11:42:57
PHP
Inyardic, 2015-10-28 11:42:57

Proper exception handling?

Hello. There is a wrapper class above the class. In my wrapper, I provide for several cases where things can go wrong - invalid data was passed, the original class returned incorrect data, and so on. Each such case throws a different exception.
The question is how to catch them correctly when using a class? Catch the basic exception, or catch each specific one with a ladder?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
V
Vyacheslav, 2015-10-28
@vacslav-dev

Ladder to catch from more specific to more general. The most general is intercepted last.

A
Alexey Ukolov, 2015-10-28
@alexey-m-ukolov

The question is how to catch them correctly when using a class? Catch the basic exception, or catch each specific one with a ladder?

The whole point of using special types of exceptions is that for each abnormal situation you can implement your own processing logic.

D
Dmitry Kovalsky, 2015-10-28
@dmitryKovalskiy

Depends on the exception handling logic. If you want to choke them just so that they don’t fly out to the client, but log them in catch, then catching a general exception will do. If you have the opposite - the most diverse set of handlers for such situations - then the approach is "from the particular to the general from top to bottom."

A
Alexey Cheremisin, 2015-10-28
@leahch

Now they will throw stones at me... But exceptions are evil!
They consume a lot of time and resources, often nothing can be fixed, the execution context is torn, etc.
So,
1) Do checks, use defaults wherever possible.
2) If an exception has already occurred, then process it immediately, at the same level.
3) Don't get carried away creating your own exceptions! In 95% of cases, the situation can be eliminated without them! And in the remaining 5%, standard system ones are enough.
4) All uninterruptible exceptions - handle at the upper levels Here
is what I wanted to say - java-performance.info/throwing-an-exception-in-jav...

A
alexxandr, 2015-10-28
@alexxandr

try {
....
} catch (exception & e) {
std::cout << "Everything is absolutely OK!" << std::endl;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question