既然所有错误都是异常,那么PHP7上的set_error_handler()会发生什么? [英] What happens with set_error_handler() on PHP7 now that all errors are exceptions?

查看:159
本文介绍了既然所有错误都是异常,那么PHP7上的set_error_handler()会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP5上,同时定义set_exception_handler()set_error_handler()完全有意义.

On PHP5 it makes a whole lot of sense having both set_exception_handler() and set_error_handler() defined.

但是,在PHP7上,所有(大多数?)错误现在都是例外.那么,定义两个处理程序的意义何在,即使异常处理程序甚至传递错误也是如此?

However, on PHP7 all (most?) errors are now exceptions. So, what's the point on defining both handlers, if even errors would pass by the exception handler instead?

我看到异常处理程序文档中有关于PHP7新的Error类的注释,但是没有引用错误处理程序功能中除了Throwable之外不再存在普通错误的事实.

I see there's a note on PHP7 new Error class in the exception handler doc, but there's no reference to the fact there's no plain errors anymore, but Throwables, in the error handler function.

从PHP 7开始,大多数错误都是通过抛出Error异常来报告的,该异常也会被处理程序捕获. ErrorException均实现Throwable接口. [来源]

Since PHP 7, most errors are reported by throwing Error exceptions, which will be caught by the handler as well. Both Error and Exception implements the Throwable interface. [source]

推荐答案

Aaron Piotrowski(创建新的Error-Exception系统的人)的

Aaron Piotrowski (the guy who made the new Error-Exception system) has a great blog on this. I think the key point you need to understand is this

在PHP 7中,当发生致命且可恢复的错误(E_ERRORE_RECOVERABLE_ERROR)时,将引发异常,而不是中止脚本执行.在某些情况下(例如内存不足)仍然存在致命错误,并且仍然会通过立即停止脚本执行来像以前一样执行致命错误.未捕获的异常在PHP 7中也将继续是致命错误.这意味着,如果因PHP 5.x中致命的错误引发的异常未被捕获,则在PHP 7中仍将是致命错误.

In PHP 7, an exception will be thrown when a fatal and recoverable error (E_ERROR and E_RECOVERABLE_ERROR) occurs, rather than halting script execution. Fatal errors still exist for certain conditions, such as running out of memory, and still behave as before by immediately halting script execution. An uncaught exception will also continue to be a fatal error in PHP 7. This means if an exception thrown from an error that was fatal in PHP 5.x goes uncaught, it will still be a fatal error in PHP 7.

请注意,其他类型的错误(如警告和注意)在PHP 7中保持不变.只有致命和可恢复的错误才会引发异常.

Note that other types of errors such as warnings and notices remain unchanged in PHP 7. Only fatal and recoverable errors throw exceptions.

换一种方式考虑一下

  • set_exception_handler()-默认情况下处理Exception的函数(自PHP 7.0起,它可以处理所有Throwable s,因此可以捕获可恢复的错误)
  • set_error_handler()-处理可恢复错误的功能
  • set_exception_handler() - Function to handle Exceptions by default (as of PHP 7.0 this can handle all Throwables, so it can catch recoverable errors)
  • set_error_handler() - Function to handle recoverable errors

换句话说,它们的功能没有改变.在PHP5中触发它们的任何东西都将在PHP7中触发它们,仅此而已,现在,您可以在脚本级别使用try-catch块来处理特定的错误.

In other words, their functionality didn't change. Anything that triggers them in PHP5 will trigger them in PHP7, it's just that, now, you can use a try-catch block at the script level to handle a specific error.

这篇关于既然所有错误都是异常,那么PHP7上的set_error_handler()会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆