是否可以仅对部分脚本禁用错误报告?
Is it possible to disable error reporting only for part of your script?
PHP 文档 reads(强调我的):
The error_reporting() function sets the error_reporting directive at
runtime. PHP has many levels of errors, using this function sets that
level for the duration (runtime) of your script. If the optional level
is not set, error_reporting() will just return the current error
reporting level.
是否意味着在PHP脚本中报错级别只能设置一次,之后就不能更改了?例如,是否可以这样做:
error_reporting(0);
try {
errorSilently();
} catch (Exception $e) {
// Do nothing
}
error_reporting(E_ALL);
try {
errorLOUDLY();
} catch (Exception $e) {
// Do nothing
}
请注意,我写了一个 // Do nothing
,因为似乎如果抛出错误,Apache 会以任何方式将其写入 error_log
,无论是否被捕获。我想做的是禁用该行为,而不是写入错误日志。
是的,您可以在程序运行期间随时更改错误报告级别,它将保持在新级别,直到您再次更改它。您 可以 "silence" 只有特定部分。请注意 error_reporting()
returns 旧 级别,具体来说,您可以执行以下操作:
$previous = error_reporting(0);
something_which_produces_warnings_which_can_safely_be_ignored();
error_reporting($previous);
要同时暂时禁用错误日志记录,您可能需要ini_set('log_errors', false)
。
不,try..catch
与错误报告 无关。这些是独立的机制。 error_reporting
影响由 trigger_error
触发的错误; try..catch
是一种处理通过 throw
.
抛出的异常的机制
PHP 文档 reads(强调我的):
The error_reporting() function sets the error_reporting directive at runtime. PHP has many levels of errors, using this function sets that level for the duration (runtime) of your script. If the optional level is not set, error_reporting() will just return the current error reporting level.
是否意味着在PHP脚本中报错级别只能设置一次,之后就不能更改了?例如,是否可以这样做:
error_reporting(0);
try {
errorSilently();
} catch (Exception $e) {
// Do nothing
}
error_reporting(E_ALL);
try {
errorLOUDLY();
} catch (Exception $e) {
// Do nothing
}
请注意,我写了一个 // Do nothing
,因为似乎如果抛出错误,Apache 会以任何方式将其写入 error_log
,无论是否被捕获。我想做的是禁用该行为,而不是写入错误日志。
是的,您可以在程序运行期间随时更改错误报告级别,它将保持在新级别,直到您再次更改它。您 可以 "silence" 只有特定部分。请注意 error_reporting()
returns 旧 级别,具体来说,您可以执行以下操作:
$previous = error_reporting(0);
something_which_produces_warnings_which_can_safely_be_ignored();
error_reporting($previous);
要同时暂时禁用错误日志记录,您可能需要ini_set('log_errors', false)
。
不,try..catch
与错误报告 无关。这些是独立的机制。 error_reporting
影响由 trigger_error
触发的错误; try..catch
是一种处理通过 throw
.