问题描述
|
我认为这有点奇怪。
我正在运行一个脚本,该脚本运行一个程序。我将最新输出存储在变量中。如果程序或我的脚本由于不同的原因在脚本中导致异常,我将捕获该异常,附加错误日志,然后将其重新抛出。在上面的几层中,我再次捕获了该消息,记录了总消息,然后将其重新抛出以关闭脚本。
但是,当PHP命令行死亡时,似乎没有在堆栈跟踪中打印内部异常消息,因为没有提及我的消息。它甚至说Uncaught \“ Exception \”是我在测试时抛出的内部异常,而外部异常是自定义类型。 getMessage()返回预期的完整消息。
PHP Fatal error: Uncaught exception \'Exception\' with message \'This is a test error message.\' in /home/x/Documents/project/file.PHP:14
堆栈跟踪:
我疯了,这是预期的行为吗?我应该去睡觉吗?
更新:
在“根”级别,我有以下内容:
catch(Exception $e)
{
$access->logError($e->getMessage());
print \'Actual error: \' . $e->getMessage() . \"\\n\";
print get_class($e);
throw $e;
}
打印语句打印我期望的真实消息,即汇总。抛出$ e会产生内部异常消息...print get_class($e)
显示CustomException ...
这是CustomException的声明:
class CustomException extends Exception
{
function __construct($message,$prev)
{
parent::__construct($message,-1,$prev);
}
}
使用方法如下:
catch(Exception $e)
{
$message = $e->getMessage() . $logString;
$resultReporter->reportError($message);
$e = new CommandException($message,$e);
$throw e;
}
解决方法
毕竟,我想这是预期的行为。
PHP似乎要做的就是在有机会显示异常时向后向上处理异常堆栈。
请参阅以下输出:
Rudis-Mac-Pro:~ rudi$ php ex.php
PHP Fatal error: Uncaught exception \'Exception\' with message \'I\'m an inner exception\' in /Users/rudi/ex.php:3
Stack trace:
#0 {main}
Next exception \'Exception\' with message \'I\'m the outer exception\' in /Users/rudi/ex.php:6
Stack trace:
#0 {main}
Next exception \'Exception\' with message \'I\'m the reeally outer exception\' in /Users/rudi/ex.php:8
Stack trace:
#0 {main}
thrown in /Users/rudi/ex.php on line 8
这是由相当基本的代码产生的:
<?php
try {
throw new Exception(\"I\'m an inner exception\");
} catch(Exception $ex) {
try {
throw new Exception(\"I\'m the outer exception\",-1,$ex);
} catch(Exception $iex) {
throw new Exception(\"I\'m the reeally outer exception\",$iex);
}
}
这样做可能是为了使开发人员可以更好地了解实际情况,而不是抽象的情况。如果考虑一下,这有点道理^ _ ^
现在上床睡觉;-)