gpt4 book ai didi

php - register_shutdown_function() 和 set_error_handler() 可以捕获相同的错误吗?

转载 作者:行者123 更新时间:2023-12-03 07:40:13 27 4
gpt4 key购买 nike

如果我在同一个脚本中定义了以下内容:

register_shutdown_function('handlerOne');
set_error_handler('handlerTwo');

是否有任何错误类型会触发这两个处理程序?

最佳答案

shutdown function将在脚本执行完成时执行,无论是否有错误、异常。它与错误或异常无关,错误或异常不会触发它,它不会捕获它们,无论如何都会调用它在脚本的末尾,因此如果您想在发生异常或 fatal error 的情况下进行一些工作,这很有用,因为如果发生 fatal error 或异常,错误处理函数不会被执行。

error handler function将在触发错误时执行。这是从手册中引用的

The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called.


<?php

function shutdownFunction(){
echo "shutdownFunction is called \n";
}

function errorHandlerFunction(){
echo "errorHandlerFunction is called \n";
}
register_shutdown_function('shutdownFunction');
set_error_handler('errorHandlerFunction');

//echo "foo\n"; // scenario 1 no errors
//echo $undefinedVar; //scenario 2 error is triggered
//undefinedFunction(); //scenario 3 Fatal error is triggered
//throw new \Exception(); //scenario 4 exception is thrown

场景 1(无错误)输出
foo 
shutdownFunction is called

场景 2(触发错误)输出
errorHandlerFunction is called 
shutdownFunction is called

场景 3(触发 fatal error )输出
Fatal error: Call to undefined function undefinedFunction() in /tmp/execpad-b2a446c7f6a6/source-b2a446c7f6a6 on line 15
shutdownFunction is called

场景 4(抛出异常)输出
Fatal error: Uncaught exception 'Exception' in /tmp/execpad-0b3a18f0ea06/source-0b3a18f0ea06:16
Stack trace:
#0 {main}
thrown in /tmp/execpad-0b3a18f0ea06/source-0b3a18f0ea06 on line 16
shutdownFunction is called

自己看 https://eval.in/1073642

关于php - register_shutdown_function() 和 set_error_handler() 可以捕获相同的错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54005833/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com