Are exceptions in php really that useful?

后端 未结 9 2329
猫巷女王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:47

    Exceptions as an error handling mechanism are VERY different in concept and implementation than function return codes. You cannot/should not simply map one to the other. You should read and digest this article (and then a few more including this one*) before proceeding further.

    If you're going to favor exceptions instead of return codes for error reporting/handling then the structure of your code should change significantly.

    (*The CodeProject link is .NET-specific but there's little code ti digest. It's mostly a best-practices article easily applicable to any language.)

    0 讨论(0)
  • 2021-02-08 15:50

    Exceptions' usefullness is not in printing error codes. It's in catching error so you can try to solve them instead of crashing with fireworks.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题