【发布时间】:2011-02-11 05:37:47
【问题描述】:
我有一个带有 show() 方法的自定义异常类,它以漂亮的方式显示异常。
在一些致命错误中,我没有捕获异常,因此程序中间存在,但我希望使用 show 方法显示这些异常。
这个可以吗?
class MyException extends Exception
{
public $e;
public function _contruct($message, $code = 0)
{
parent::_construct($message, $code);
}
public function show()
{
if(!defined('DEBUG')) define('DEBUG', FALSE);
p("Error: " . $this->getMessage());
if(DEBUG)
{
p('Stack trace');
p($this->getFile() . ' (' . $this->getLine() . ')');
pre(get_dump($this->getTraceAsString()));
}
}
}
解决方案
class MyException extends Exception
{
public $e;
public function _contruct($message, $code = 0)
{
parent::_construct($message, $code);
}
public function show()
{
MyException::realShow($this);
}
static function realShow($e)
{
if(!defined('DEBUG')) define('DEBUG', FALSE);
p("Error: " . $e->getMessage());
if(DEBUG)
{
p('Stack trace');
p($e->getFile() . ' (' . $e->getLine() . ')');
pre(get_dump($e->getTraceAsString()));
}
}
}
如果有人有更好的方法,我是所有的眼睛。
【问题讨论】:
标签: php oop exception exception-handling