PHP5的异常处理机制[10]--同时处理多个错误

王朝php·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

处理多个错误

在目前为止异常处理看起来和我们传统的作法—检验返回的错误标识或对象的值没有什么太大区别。让我们将CommandManager处理地更谨慎,并在构造函数中检查command目录是否存在。

index_php5_2.php

<?php

// PHP 5

require_once('cmd_php5/Command.php');

class CommandManager {

private $cmdDir = "cmd_php5";

function __construct() {

if (!is_dir($this->cmdDir)) {

throw new Exception(

"directory error: $this->cmdDir");

}

}

function getCommandObject($cmd) {

$path = "{$this->cmdDir}/{$cmd}.php";

if (!file_exists($path)) {

throw new Exception("Cannot find $path");

}

require_once $path;

if (!class_exists($cmd)) {

throw new Exception("class $cmd does not exist");

}

$class = new ReflectionClass($cmd);

if (!$class->isSubclassOf(new ReflectionClass('Command'))) {

throw new Exception("$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的后半段完全相同。

<?php

// PHP 5

try {

$mgr = new CommandManager(); // potential 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

<?php

// PHP 5

require_once('cmd_php5/Command.php');

class CommandManager {

private $cmdDir = "cmd_php5";

const CMDMAN_GENERAL_ERROR = 1;

const CMDMAN_ILLEGALCLASS_ERROR = 2;

function __construct() {

if (!is_dir($this->cmdDir)) {

throw new Exception("directory error: $this->cmdDir", self::CMDMAN_GENERAL_ERROR);

}

}

function getCommandObject($cmd) {

$path = "{$this->cmdDir}/{$cmd}.php";

if (!file_exists($path)) {

throw new Exception("Cannot find $path", self::CMDMAN_ILLEGALCLASS_ERROR);

}

require_once $path;

if (!class_exists($cmd)) {

throw new Exception("class $cmd does not exist", self::CMDMAN_ILLEGALCLASS_ERROR);

}

$class = new ReflectionClass($cmd);

if (!$class->isSubclassOf(new ReflectionClass('Command'))) {

throw new Exception("$cmd is not a Command", self::CMDMAN_ILLEGALCLASS_ERROR);

}

return $class->newInstance();

}

}

?>

通过传递 CMDMAN_ILLEGALCLASS_ERROR和 CMDMAN_GENERAL_ERROR其中之一的参数给我们抛出的异常对象,我们就可以让客户代码区分不同类型的错误,并定义不同的处理策略。

index_php5_3.php

<?php

// PHP 5

try {

$mgr = new CommandManager();

$cmd = $mgr->getCommandObject('realcommand');

$cmd->execute();

} catch (Exception $e) {

if ($e->getCode() == CommandManager::CMDMAN_GENERAL_ERROR) {

// no way of recovering

die($e->getMessage());

} else if ($e->getCode() == CommandManager::CMDMAN_ILLEGALCLASS_ERROR) {

error_log($e->getMessage());

print "attempting recovery\n";

// perhaps attempt to invoke a default command?

}

}

?>

我们也可以用另一种方法来实现这样的效果—从最根本的Exception类中派生出代表不同类型异常的子类,再抛出和捕捉。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航