假设我们使用自定义的Exception类扩展来处理自定义异常,例如:
$testObject = new testClass();
并且像这样自动加载:
function __autoload($class_name) {
$file = $class_name.'.php';
if (file_exists($file)) {
include $file;
}else{
throw new loadException("File $file is missing");
}
if(!class_exists($class_name,false)){
throw new loadException("Class $class_name missing in $file");
}
return true;
}
try {
$testObject = new testClass();
}catch(loadException $e){
exit('<pre>'.$e.'</pre>');
}
文件testClass.php不存在,因此调用了loadException并显示消息:
文件testClass.php丢失。(以及所有其他细节...行号等)
一切都很好,直到我决定隐藏所有错误并显示404页(或500页...),所以我想添加一个loadErrorPage函数。
class loadException {
...
function loadErrorPage($code){
$page = new pageClass();
echo $page->showPage($code);
}
}
...
try {
$testObject = new testClass();
}catch(loadException $e){
$e->loadErrorPage(500);
}
但是这种方法存在一个明显的问题,如果testClass.php和pageClass.php文件都不存在,那么会显示致命错误而不是我们期望的404页面。
我有些困惑 :S 如何在异常处理程序中优雅地处理这个异常?