php - 如何正确设置PHP set_error_handler

标签 php error-handling custom-error-handling

您能帮我正确设置php set_error_handler 吗?我需要处理引发php 警告作为异常的代码。但是我不知道该怎么做。现在我有了这段代码,我认为这是不正确的。

set_error_handler( function( $errno, $errstr, $errfile, $errline, array $errcontext )
{
    throw new \Exception( $errstr, $errno );
});
$mailer->send( $mail );
restore_error_handler(); 

正如我在documentation中看到的那样,它需要更复杂的解决方案。但是我对所有这些常数都不太困惑。
有没有一种以某种优雅的方式设置它的方法,它不会单独设置所有常量。
我的意思是:
function( $errno, $errstr, $errfile, $errline, array $errcontext )
{
    if( $errno >= E_USER_WARNING ) throw new \Exception( $errstr, $errno );
});

因此,如何以某种优雅的方式包含php警告Exception。谢谢。

最佳答案

因此,在我的情况下,必须满足errno的条件,并且必须返回true。

set_error_handler( function( $errno, $errstr, $errfile, $errline, array $errcontext )
{
    // Cause Php does not catch stream_socket_client warnings message.
    if ( $errno == E_WARNING ) throw new \Exception( $errstr, $errno ); 
    // Is necessary to handle other errors by default handler.
    return true; 
}); 

关于php - 如何正确设置PHP set_error_handler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40574576/

相关文章:

r - 如何在barplot中手动放置错误行?

angular - 如何捕获Angular 6解析器中的错误?

PHP MySQL 多个图像 URL 字段数组

php - undefined variable : image_event_id

php - 使用 ZF2 时重定向到公用文件夹

php - Laravel 明显没有做好它的工作

php - .htaccess中的php_value显示内部服务器错误

java - 自定义服务器错误代码 springMVC 和 tomcat

error-handling - 以干净有效的方式处理 Lua 错误

javascript - 自定义错误类作为 Node 模块发送自定义错误响应