Chinaunix首页 | 论坛 | 博客
  • 博客访问: 14424773
  • 博文数量: 5645
  • 博客积分: 9880
  • 博客等级: 中将
  • 技术积分: 68081
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-28 13:35
文章分类

全部博文(5645)

文章存档

2008年(5645)

我的朋友

分类:

2008-04-28 20:32:26

下载本文示例代码
  建立一个Exception对象后你可以将对象返回,但不应该这样使用,更好的方法是用throw关键字来代替。throw用来抛出异常: throw new Exception( "my message", 44 );   throw 将脚本的执行中止,并使相关的Exception对象对客户代码可用。  以下是改进过的getCommandObject() 方法:  index_php5.php <?php  // PHP 5  require_once('cmd_php5/Command.php');  class CommandManager {   private $cmdDir = "cmd_php5";   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();   }  } ?>  代码中我们使用了PHP5的反射(Reflection)API来判断所给的类是否是属于Command 类型。在错误的路径下执行本脚本将会报出这样的错误: Fatal error: Uncaught exception 'Exception' with message 'Cannot find command/xrealcommand.php' in /home/xyz/BasicException.php:10 Stack trace: #0 /home/xyz/BasicException.php(26): CommandManager->getCommandObject('xrealcommand') #1 {main} thrown in /home/xyz/BasicException.php on line 10   默认地,抛出异常导致一个fatal error。这意味着使用异常的类内建有安全机制。而仅仅使用一个错误标记,不能拥有这样的功能。处理错误标记失败只会你的脚本使用错误的值来继续执行。 编辑推荐:PHP 5.0对象模型深度探索   建立一个Exception对象后你可以将对象返回,但不应该这样使用,更好的方法是用throw关键字来代替。throw用来抛出异常: throw new Exception( "my message", 44 );   throw 将脚本的执行中止,并使相关的Exception对象对客户代码可用。  以下是改进过的getCommandObject() 方法:  index_php5.php <?php  // PHP 5  require_once('cmd_php5/Command.php');  class CommandManager {   private $cmdDir = "cmd_php5";   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();   }  } ?>  代码中我们使用了PHP5的反射(Reflection)API来判断所给的类是否是属于Command 类型。在错误的路径下执行本脚本将会报出这样的错误: Fatal error: Uncaught exception 'Exception' with message 'Cannot find command/xrealcommand.php' in /home/xyz/BasicException.php:10 Stack trace: #0 /home/xyz/BasicException.php(26): CommandManager->getCommandObject('xrealcommand') #1 {main} thrown in /home/xyz/BasicException.php on line 10   默认地,抛出异常导致一个fatal error。这意味着使用异常的类内建有安全机制。而仅仅使用一个错误标记,不能拥有这样的功能。处理错误标记失败只会你的脚本使用错误的值来继续执行。 编辑推荐:PHP 5.0对象模型深度探索 下载本文示例代码


PHP5的异常处理机制之使用throw关键字PHP5的异常处理机制之使用throw关键字PHP5的异常处理机制之使用throw关键字PHP5的异常处理机制之使用throw关键字PHP5的异常处理机制之使用throw关键字PHP5的异常处理机制之使用throw关键字PHP5的异常处理机制之使用throw关键字PHP5的异常处理机制之使用throw关键字PHP5的异常处理机制之使用throw关键字PHP5的异常处理机制之使用throw关键字PHP5的异常处理机制之使用throw关键字PHP5的异常处理机制之使用throw关键字PHP5的异常处理机制之使用throw关键字PHP5的异常处理机制之使用throw关键字PHP5的异常处理机制之使用throw关键字
阅读(221) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~