Converting errors to exceptions: design flaw?

后端 未结 3 1204
梦毁少年i
梦毁少年i 2021-01-02 06:05

I came across some code recently that used a custom error handler to turn any PHP errors into an generalized application exception. A custom exception handler was also defin

3条回答
  •  无人及你
    2021-01-02 06:24

    There has been some debate about this in the PHP community. I believe the general thinking is that errors are things thrown by internal PHP functions, and are coding problems you really need to fix; whereas exceptions are things thrown by your application code that you may just need to 'handle'.

    However some of the newer SPL extensions (which tend to be more object oriented) do use exceptions for things previously might have thrown errors, so there is some grey area.

    There's also a PHP core class ErrorException - http://www.php.net/manual/en/class.errorexception.php which looks a little easier to use than your code example, if this is a route you want to go down.

提交回复
热议问题