Are exceptions in php really that useful?

后端 未结 9 2328
猫巷女王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条回答
  •  执念已碎
    2021-02-08 15:50

    I personally hate exceptions. I don't work with them in my applications. I prefer functions returning (and expecting) defined status codes, and dealing with recoverable errors on that level.

    In truly exceptional situations (like an unreachable database server, a file I/O error etc.) that are an immediate emergency, I tend to trigger and handle a fatal error. (Object shutdown will still take place, so any connections that need closing etc. will still be handled as long as they are placed in destructor functions.)

    Third party libraries' exceptions I strive to catch as quickly as possible, and deal with them in my own way.

    Joel Spolsky puts the reasons much better than I could in his notorious Exceptions essay.

    Note that this is one view and one school of thought. There is a lot of brilliant software whose error handling is based entirely on exceptions, and that is perfectly fine. The key is consistence - either you make the design decision to use them, or you don't.

提交回复
热议问题