@a5635268
2016-04-26T03:18:07.000000Z
字数 2144
阅读 1411
PHP SPL
了解SPL异常之前,我们先了解一下嵌套异常。嵌套异常顾名思义就是异常里面再嵌套异常,一个异常抛出,在catch到以后再抛出异常,这时可以通过Exception基类的getPrevious方法可以获得嵌套异常;
<?phpclass DBException extends Exception{}class Database{/*** @var PDO object setup during construction*/protected $_pdoResource = null;public function executeQuery($sql){try {throw new PDOException('PDO error');} catch (PDOException $e) {throw new DBException('Query was unexecutable', null, $e);}return $numRows;}}try {$sql = 'select * from user';$db = new Database('PDO', $connectionParams);$db->executeQuery($sql);} catch (DBException $e) {echo 'General Error: ' . $e->getMessage() . "\n";// 调用被捕获异常的getPrevious()获得嵌套异常$pdoException = $e->getPrevious();echo 'PDO Specific error: ' . $pdoException->getMessage() . "\n";}
简要的说一下SPL异常的优点:
SPL中有总共13个新的异常类型。其中两个可被视为基类:逻辑异常(LogicException )和运行时异常(RuntimeException);两种都继承php异常类。其余的方法在逻辑上可以被拆分为3组:动态调用组,逻辑组和运行时组。
动态调用组包含异常 BadFunctionCallException和BadMethodCallException,BadMethodCallException是BadFunctionCallException(LogicException的子类)的子类。
// OO variantclass Foo{public function __call($method, $args){switch ($method) {case 'doBar': /* ... */ break;default:throw new BadMethodCallException('Method ' . $method . ' is not callable by this object');}}}// procedural variantfunction foo($bar, $baz) {$func = 'do' . $baz;if (!is_callable($func)) {throw new BadFunctionCallException('Function ' . $func . ' is not callable');}}
逻辑(logic )组包含异常: DomainException、InvalidArgumentException、LengthException、OutOfRangeException组成。
运行时(runtime )组包含异常:
它由OutOfBoundsException、OverflowException、RangeException、UnderflowException、UnexpectedValueExceptio组成。
class Foo{protected $number = 0;protected $bar = null;public function __construct($options){/** 本方法抛出LogicException异常 **/}public function setNumber($number){/** 本方法抛出LogicException异常 **/}public function setBar(Bar $bar){/** 本方法抛出LogicException异常 **/}public function doSomething($differentNumber){if ($differentNumber != $expectedCondition) {/** 在这里,抛出LogicException异常 **/}/*** 在这里,本方法抛出RuntimeException异常*/}}
http://cn.php.net/manual/zh/class.exception.php
http://cn.php.net/manual/zh/spl.exceptions.php
http://www.oschina.net/translate/exception-best-practices-in-php-5-3
