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

处理多个错误

在目前为止异常处理看起来和我们传统的作法—检验返回的错误标识或对象的值没有什么太大区别。让我们将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类中派生出代表不同类型异常的子类,再抛出和捕捉。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值