Exception类的子类

有两个理由让我们想要从Exception类中派生中子类:

1.  让子类提供自定义的功能;

2.  区分不同类型的异常;

看第二个例子。使用CommandManager类时我们可能会产生两个错误:一个是一般性的错误如找不到目录,另一个是找不到或无法生成Command对象。这样我们需要针对这两个错误来定义两种异常子类型。

index_php5_4.php

<?php
// PHP 5
require_once("cmd_php5/Command.php");
class
CommandManagerException extends Exception{}
class
IllegalCommandException extends Exception{}

class CommandManager {
    private
$cmdDir = "cmd_php5";

    function __construct() {
        if (!
($this->cmdDir)) {
            throw new
CommandManagerException("directory error: $this->cmdDir");
        }
    }

    function getCommandObject($cmd) {
        
$path = "{$this->cmdDir}/{$cmd}.php";
        if (!
($path)) {
            throw new
IllegalCommandException("Cannot find $path");
        }
        require_once
$path;
        if (!
($cmd)) {
            throw new
IllegalCommandException("class $cmd does not exist");
        }

        $class = new ReflectionClass($cmd);
        if (!
$class->isSubclassOf(new ReflectionClass("Command"))) {
            throw new
IllegalCommandException("$cmd is not a Command");
        }
        return
$class->newInstance();
    }
}
?>

当我们的类不能找到正确的command目录时,将抛出一个CommandManagerException异常;当在生成Command对象时产生错误,则getCommandObject()方法将抛出一个IllegalCommandException异常。注意存在多个可能导致抛出IllegalCommandException异常的原因(如未找到文件,或在文件中未找到正确的类)。我们将前两个例子结合起来并为IllegalCommandException提供整型的错误标识常量来代表不同类型的出错原因。

现在CommandManager类已经具备了处理这多种出错情况的能力,我们可以增加新的catch语句来匹配不同的错误类型

index_php5_4.php 后半段

<?php
// PHP 5
try {
    
$mgr = new CommandManager();
    
$cmd = $mgr->getCommandObject("realcommand");
    
$cmd->execute();
} catch (
CommandManagerException $e) {
    die(
$e->getMessage());
} catch (
IllegalCommandException $e) {
    
($e->getMessage());
    print
"attempting recovery\n";
    
// perhaps attempt to invoke a default command?
} catch (Exception $e) {
    print
"Unexpected exception\n";
    die(
$e->getMessage());
}
?>

如果CommandManager 对象抛出一个CommandManagerException异常,则相对应的catch语句将会执行。每个catch语句的参数就像是一个匹配测试一样,第一个发生匹配的catch语句将会执行,而不执行其它的catch语句。所以,你应当将针对特定异常的catch语句写在前面,而将针对一般性的异常的catch语句写在后面。

如果你将catch语句这样写:

<?php
// PHP 5
try {
    
$mgr = new CommandManager();
    
$cmd = $mgr->getCommandObject("realcommand");
    
$cmd->execute();
} catch (
Exception $e) {
    print
"Unexpected exception\n";
    die(
$e->getMessage());
} catch (
CommandManagerException $e) {
    die(
$e->getMessage());
} catch (
IllegalCommandException $e) {
    
($e->getMessage());
    print
"attempting recovery\n";
    
// perhaps attempt to invoke a default command?
}
?>

那么当异常抛出时,不管是什么异常第一个catch语句catch (Exception $e){}将总是被执行。这是由于任何异常都从属于Exception类型,所以总是匹配。这就达不到我们所要的针对特定异常进行不同处理的目的。

如果你在捕捉特定类型的异常,那么在最后一个catch语句中捕捉Exception类型的异常是一个好主意。最后一个catch语句表示catch-all,捕捉所有异常。当然,你可能不想马上处理异常,而是想要将它传递,然后在适当的时候处理。这是PHP的异常机制中另一个需要讨论的地方。

Source national conference of state legislatures in june, the advisory write my college essay committee on immunization practices of the centers for disease control and prevention in atlanta recommended the vaccine in females ages 9 to 26 Source national conference of state legislatures in june, the advisory write my college essay committee on immunization practices of the centers for disease control and prevention in atlanta recommended the vaccine in females ages 9 to 26 I totally agree that these are do essay writing websites work such essential skills

标签:
本文连接地址: http://www.fresker.com/archives/413 (转载注明出处)

回复

目前暂无评论

Sorry, 评论已关闭.