【发布时间】:2012-02-08 17:42:08
【问题描述】:
我使用 PHP 手册中的一些代码进行异常测试,但收到一些奇怪的消息。
代码如下:
function inverse($x) {
if (!$x) {
throw new Exception('Division by zero.');
}
else return 1 / $x;
}
try {
echo inverse(5) . "\n";
echo inverse(0) . "\n";
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
// Continue execution
echo 'Hello World';
这是输出:
0.2
( ! ) Exception: Division by zero. in /var/www/OOPlearing/slash.php on line 10Call Stack#TimeMemoryFunctionLocation10.0002330188{main}( )../slash.php:020.0002330232inverse( $x = 0 )../slash.php:17Dump $_SERVER$_SERVER['HTTP_HOST'] =string 'localhost' (length=9)$_SERVER['SERVER_NAME'] =string 'localhost' (length=9)Dump $_GETVariables in local scope (#2)$x =int 0
Caught exception: Division by zero.Hello World
奇怪的是,虽然已经捕获到异常,但是异常信息还在...
我在 php.ini 中的一些本地设置:
error_reporting = E_ALL & ~E_DEPRECATED
display_errors =On
display_startup_errors = Off
log_errors = Off
......
html_errors = On
我的笔记本:
ubuntu11.04
mysql Ver 14.14 Distrib 5.1.54
PHP 5.3.5-1
Update(2012.1.16) 导致此类错误输出的是 xdebug 扩展。 xdebug 默认显示错误情况下的堆栈跟踪。对于那些想要禁用它的人可以按照以下说明进行操作:
xdebug_disable();//put it on the header of your code,like cofig file.
【问题讨论】:
-
您是否尝试将
display_errors设置为关闭? -
打开错误报告不是解决问题的方法;)
-
我复制粘贴你的代码,它只显示 0.2 捕获的异常:除以零。你好世界。所以没问题
-
@rkosegi 这是由于 php 配置中错误报告的额外冗长......所以它实际上不是问题,它是它的工作方式......你的错误报告配置是什么php.ini?
-
@jere:你是对的,我的错误,error_reporting = E_ALL,但 display_error = Off
标签: php exception error-handling