Are exceptions in php really that useful?

后端 未结 9 2314
猫巷女王i
猫巷女王i 2021-02-08 14:48

3 days ago I started rewriting one of my scripts in OOP using classes as a practice after reading a lot about the advantages of using OOP.

Now I\'m confused weather I sh

9条回答
  •  闹比i
    闹比i (楼主)
    2021-02-08 15:33

    There is a finite capacity to most development processes - i.e. even where its possible to predict all possible circumstances that the code might run in (i.e. all possible combinations of inputs, all possible states for supporting systems like database, DNS, existing data etc) then its just not practical to deal with every scenario. Using exceptions allows you:

    1. to bundle a series of operations into one entity for the purposes of determining success/ failure as a whole

    2. handle multiple different modes of failures with a single bit of code

    So yes - I'd say that exception handling is a useful practice - but not a substitute for handling the common failure modes specifically, intelligently and informatively (and typed exceptions are IMHO a complete oxymoron).

提交回复
热议问题