我有一个自定义异常类与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()));
}
}
}
如果有人有更好的办法,我是所有人的目光。
function exception_handler($ex) {
//$ex will be the thrown Exception object
}
set_exception_handler('exception_handler');
从文档:
是这样的? set_exception_handler( 'MyException ::显示'); – 2011-02-11 05:47:19