B
B
BonBon Slick2018-04-28 16:18:06
Exceptions
BonBon Slick, 2018-04-28 16:18:06

One eksepshen or many?

Which option is more correct

try {
       ...
        }
        catch (Throwable $throwable) { 
              $this->logger->error(  $throwable->getMessage()
            );
        }

or
try {
       ...
        }
        catch (Exception1 | Exception2 | Exception3 | Exception4 | Exception5 | Exception6 | Exception7 $throwable) { 
              $this->logger->error(  $throwable->getMessage()
            );
        }

Which approach is more correct? Why?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
longclaps, 2018-04-28
@BonBonSlick

First you need to understand that try / catch is not about errors, it's about control flows.
You have a main control flow - one that you build from all sorts of if'ov, for'ov, function calls and returns from them - in general, from ordinary statements.
And there is an alternative flow that allows, under certain conditions (namely, when an exception is raised), to be transferred to another point in the program, with a different state of the call stack, which in general could be done with ordinary operators, but required a lot of writing.
It may be necessary to transfer to another point in the program in order to restart from it, staying in the correct and understandable state of the program.
In order to shit in the log, you don’t need to transfer anywhere at all - exceptions usually don’t break the logging subsystem.
Enough for now.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question