PHP5的异常处理机制(二)
PHP5的异常处理机制(二)
2011年12月15日
根据我们以上讨论的,PHP内建的异常类需要有以下成员方法: __construct() 构造函数,需要一个出错信息和一个可选的整型错误标记作参数 getMessage() 取得出错信息 getCode() 出错的代码 getFile() 异常发生的文件 getLine() 异常发生的行数 getTrace() 跟踪异常每一步传递的路线,存入数组,返回该数组 getTraceAsString() 和getTrace()功能一样,但可以将数组中的元素转成字符串并按一定格式输出 可以看出来,Exception类的结构和Pear_Error很相似。当你的脚本中遇到一个错误,你可以建立你的异常对象: $ex = new Exception( "Could not open $this->file" ); Exception类的构造函数将接受一个出错信息和一个错误代码。
建立一个Exception对象后你可以将对象返回,但不应该这样使用,更好的方法是用throw关键字来代替。throw用来抛出异常: throw new Exception( "my message", 44 ); throw将脚本的执行中止,并使相关的Exception对象对客户代码可用。
以下是改进过的getCommandObject()方法 :
index_php5.php cmdDir}/{$cmd}.php";
if (!file_exists($path)) {
throw newException("Cannot find $path");
}
require_once$path;
if (!class_exists($cmd)) {
throw newException(
"class $cmd does not exist");
}
$class= newReflectionClass($cmd);
if (!$class->isSubclassOf(newReflectionClass('Command'))) {
throw newException("$cmd is not a Command");
}
return new$cmd();
}
}
?> 代码中我们使用了PHP5的反射(Reflection)API来判断所给的类是否是属于Command类型。在错误的路径下执行本脚本将会报出这样的错误: Fatal error: Uncaught exception 'Exception' with message 'Cannot find command/xrealcommand.php' in /home/xyz/BasicException.php:10
Stack trace:
#0 /home/xyz/BasicException.php(26):
CommandManager->getCommandObject('xrealcommand')
#1 {main}
thrown in /home/xyz/BasicException.php on line 10 默认地,抛出异常导致一个fatal error。这意味着使用异常的类内建有安全机制。而仅仅使用一个错误标记,不能拥有这样的功能。处理错误标记失败只会你的脚本使用错误的值来继续执行。
为了进一步处理异常,我们需要使用try-catch语句―包括Try语句和至少一个的catch语句。任何调用可能抛出异常的方法的代码都应该使用try语句。Catch语句用来处理可能抛出的异常。以下显示了我们处理getCommandObject()抛出的异常的方法: index_php5.php后半段 getCommandObject('realcommand');
$cmd->execute();
} catch (Exception $e) {
print$e->getMessage();
exit();
}
?> 可以看到,通过结合使用throw关键字和try-catch语句,我们可以避免错误标记“污染”类方法返回的值。因为“异常”本身就是一种与其它任何对象不同的PHP内建的类型,不会产生混淆。
如果抛出了一个异常,try语句中的脚本将会停止执行,然后马上转向执行catch语句中的脚本。
如果异常抛出了却没有被捕捉到,就会产生一个fatal error。
在目前为止异常处理看起来和我们传统的作法―检验返回的错误标识或对象的值没有什么太大区别。让我们将CommandManager处理地更谨慎,并在构造函数中检查command目录是否存在。 index_php5_2.php cmdDir)) {
throw newException(
"directory error: $this->cmdDir");
}
}
functiongetCommandObject($cmd) {
$path="{$this->cmdDir}/{$cmd}.php";
if (!file_exists($path)) {
throw newException("Cannot find $path");
}
require_once$path;
if (!class_exists($cmd)) {
throw newException("class $cmd does not exist");
}
$class= newReflectionClass($cmd);
if (!$class->isSubclassOf(newReflectionClass('Command'))) {
throw newException("$cmd is not a Command");
}
return new$cmd();
}
}
?> 这里有两个地方的调用可能导致程序出错(__construct() 和getCommandObject())。尽管如此,我们不需要调整我们的客户代码。你可以在try语句中增添众多内容,然后在catch中统一处理。如果CommandManager对象的构造函数抛出一个异常,则try语句中的执行中止,然后catch语句被调用捕捉相关的异常。同样地,getCommandObject()也是如此。这样,我们有同时存在两个潜在的引发错误的地方,和一个唯一的语句来处理所有的错误。这让我们的代码看起来更加整洁,又可以满足错误处理的要求。和前面提到的PHP的传统的错误方法相比,显然很有优势。 index_php5_2.php后半段
注意:尽管和index_php5.php相比,前半段代码有两个可能出错的地方,这段代码和index_php5.php的后半段完全相同。 error
$cmd=$mgr->getCommandObject('realcommand');
// another potential error
$cmd->execute();
} catch (Exception $e) {
// handle either error here
print$e->getMessage();
exit();
}
?> 还有一个地方我们没有提到。我们怎样区分不同类型的错误?例如,我们可能希望用一种方法来处理找不到目录的错误,而用另一种方法来处理非法的command类。
Exception类可以接受一个可选的整型的错误标识,这是在catch语句中区分不同错误类型的一个方法。 index_php5_3.php cmdDir)) {
throw newException("directory error: $this->cmdDir",self::CMDMAN_GENERAL_ERROR);
}
}
functiongetCommandObject($cmd) {
$path="{$this->cmdDir}/{$cmd}.php";
if (!file_exists($path)) {
throw newException("Cannot find $path",self::CMDMAN_ILLEGALCLASS_ERROR);
}
require_once$path;
if (!class_exists($cmd)) {
throw newException("class $cmd does not exist",self::CMDMAN_ILLEGALCLASS_ERROR);
}
$class= newReflectionClass($cmd);
if (!$class->isSubclassOf(newReflectionClass('Command'))) {
throw newException("$cmd is not a Command",self::CMDMAN_ILLEGALCLASS_ERROR);
}
return$class->newInstance();
}
}
?>
猜你喜欢
转载自flf592ap.iteye.com/blog/1359487
今日推荐
周排行