如果我们已经触发了一些在发生时无法马上处理的异常,有一个很好的解决方案—将处理异常的责任交回给调用当前方法的代码,也就是在catch语句中再次抛出异常(重掷异常)。这将使异常沿着方法的调用链向上传递。
index_php5_5.php
<?php
// PHP 5
class RequestHelper {
private $request = array();
private $defaultcmd = 'defaultcmd' ;
private $cmdstr ;
function __construct ( $request_array = null ) {
if (! is_array ( $this -> request = $request_array )) {
$this -> request = $_REQUEST ;
}
}
function getCommandString () {
return ( $this -> cmdstr ? $this -> cmdstr : ( $this -> cmdstr = $this -> request [ 'cmd' ]));
}
function runCommand () {
$cmdstr = $this -> getCommandString ();
try {
$mgr = new CommandManager ();
$cmd = $mgr -> getCommandObject ( $cmdstr );
$cmd -> execute ();
} catch ( IllegalCommandException $e ) {
error_log ( $e -> getMessage ());
if ( $cmdstr != $this -> defaultcmd ) {
$this -> cmdstr = $this -> defaultcmd ;
$this -> runCommand ();
} else {
throw $e ;
}
} catch ( Exception $e ) {
throw $e ;
}
}
}
$helper = new RequestHelper (array( cmd => 'realcommand' ));
$helper -> runCommand ();
?>
以上我们使用了RequestHelper 类中 的 一段客户代码。RequestHelper 用来处理用户提供的请求数据。在构造函数中我们接受一个用来 debug 的数组。如果没有接受到这个数组,类将使用 $_REQUEST 数组。无论哪个数组被使用,它都将分配给名为 $request 的变量。客户代码通过给出一个 request 数组的 cmd 元素,告知它想要执行的 command 。 getCommandString() 方法则测试一个名为 $cmdstr 的属性。如果它是空的,则方法将$request中的cmd元素的内容分配给$cmdstr,并返回值。如果不是空的,方法直接返回$cmdstr属性的值。通过这样的机制,command字符串可以在RequestHelper 类中被覆写。
在最后我们将除 IllegalCommandException 外的所有异常对象都将交给更高一级的类来延后处理。我们在最后一个 catch 语句中再次抛出异常。
} catch ( Exception $e ) {
throw $e ;
}
如果我们捕捉到一个IllegalCommandException 异常,我们首先尝试去调用 一个默认的 command 。 我们通过将 $cmdstr 属性设置为与 $defaultcmd 等值,并重复地调用 runCommand 方法。如果 $cmdstr 和 $defaultcmd 字符串已经相等,我们没有什么需要做的,则重掷异常。
事实上在 Zend引擎II将会自动重掷所有未匹配的异常,所以我们可以省略最后一个catch语句。这是CommandManager::getCommandObject() 的 最后一行:
return $class -> newInstance ();
这里要注意两个问题:
首先,我们假设CommandManager类的构造函数不需要参数。在本文中我们不讨论需要参数的情况。 其次,我们假设command类(这里是指我们自定义的realcommand)可以被实例化。如果构造函数被声明为private,这个语句将抛出一个ReflectionException 对象。如果我们没有在 RequestHelper 中处理异常,则这个异常将被传递到调用 RequestHelper 的代码中。如果一个异常被隐性地抛出,你最好在文档中说明一下,或者手动地抛出这个异常 -- 这样其他的程序员使用你的代码时容易处理可能发生的异常情况。 
|