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