博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【SPL标准库专题(10)】SPL Exceptions
阅读量:4677 次
发布时间:2019-06-09

本文共 2243 字,大约阅读时间需要 7 分钟。

嵌套异常

了解SPL异常之前,我们先了解一下嵌套异常。嵌套异常顾名思义就是异常里面再嵌套异常,一个异常抛出,在catch到以后再抛出异常,这时可以通过Exception基类的getPrevious方法可以获得嵌套异常;

executeQuery($sql);} catch (DBException $e) { echo 'General Error: ' . $e->getMessage() . "\n"; // 调用被捕获异常的getPrevious()获得嵌套异常 $pdoException = $e->getPrevious(); echo 'PDO Specific error: ' . $pdoException->getMessage() . "\n";}

SPL异常

简要的说一下SPL异常的优点:

  1. 可以为异常抛出提供分类,方便后续有选择性的catch异常;
  2. 异常语义化更具体,BadFunctionCallException一看就知道是调用错误的未定义方法抛出的错误;

SPL中有总共13个新的异常类型。其中两个可被视为基类:逻辑异常(LogicException )和运行时异常(RuntimeException);两种都继承php异常类。其余的方法在逻辑上可以被拆分为3组:动态调用组,逻辑组和运行时组。

动态调用组包含异常 BadFunctionCallException和BadMethodCallException,BadMethodCallException是BadFunctionCallException(LogicException的子类)的子类。

// OO variant class Foo {     public function __call($method, $args)     {         switch ($method) {             case 'doBar': /* ... */ break;             default:                 throw new BadMethodCallException('Method ' . $method . ' is not callable by this object');         }     }   }   // procedural variant function 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

转载于:https://www.cnblogs.com/nixi8/p/5434315.html

你可能感兴趣的文章
VS工具箱中添加DevExpress控件
查看>>
oracle 日期取 月 日
查看>>
flex 遇上white-space:nowrap的2种解决方法
查看>>
编译适用于TP-Link WR703N的OpenWRT固件
查看>>
Ubuntu16下编译linux内核,报"mkimage" command not found错的解决
查看>>
Nginx启动SSL功能,并进行功能优化,你看这个就足够了
查看>>
php 创建简单的Restful WebAPI(三)
查看>>
C#遍历DataSet中数据的几种方法总结
查看>>
linux tomcat安装以及配置
查看>>
Git——Git的简单介绍【一】
查看>>
Vue源码学习三 ———— Vue构造函数包装
查看>>
winform编程中的跨线程访问资源(转)
查看>>
自制操作系统Antz(5)——深入理解保护模式与进入方法
查看>>
Creating one array of strings in c fails ,why?
查看>>
POJ 3683 Priest John's Busiest Day(2-sa路径输出,4级)
查看>>
hdu 1244 Max Sum Plus Plus Plus(DP线性区间)
查看>>
4.unity3D 预设的一例
查看>>
XP Sp3 开机就要激活,否则无法登录windows桌面
查看>>
转:智能模糊测试工具 Winafl 的使用与分析
查看>>
初识 Fuzzing 工具 WinAFL
查看>>