分类:
2008-04-15 16:25:03
建立一个Exception对象后你可以将返回,但不应该这样使用,更好的是用throw关键字来代替。throw用来抛出异常:
throw new Exception( "my message", 44 ); |
<?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(); } } ?> |
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 |