php中的Exception
如果定制的EXCEPTION搞定了,默认的,就自然不在话下罗。
直接上最曲折的过程。
InvalidIdException.php
<?php namespace Bookstore\Exceptions; use Exception; class InvalidIdException extends Exception { public function __construct($message = null) { $message = $message ?: 'Invalid id provided.'; parent::__construct($message); } } ?>
ExceedeMaxAllowedException.php
<?php namespace Bookstore\Exceptions; use Exception; //从默认的Exception继承 class ExceedeMaxAllowedException extends Exception { //只有构造函数 public function __construct($message = null) { //PHP新语法,如果$message为空,则取默认值 $message = $message ?: 'Exceeded max allowed.'; //调用父类构造函数,传递消息 parent::__construct($message); } } ?>
Unique.php
<?php namespace Bookstore\Utils; use Bookstore\Exceptions\InvalidIdException; use Bookstore\Exceptions\ExceedeMaxAllowedException; require_once __DIR__ . '/InvalidIdException.php'; require_once __DIR__ . '/ExceedeMaxAllowedException.php'; trait Unique { //static静态属性,类似于其它语言的类变量 private static $lastId = 0; //protected保护属性,仅允许继承类访问 protected $id; public function setId(int $id) { if ($id < 0) { //抛出自定义异常 throw new InvalidIdException('Id cannot be negative.'); } //内置函数判断id是否为空,比$id == null要逼格好点 if (empty($id)) { //注意区分$this-和self::$的语法 $this->id = ++self::$lastId; } else { $this->id = $id; if ($id > self::$lastId) { self::$lastId = $id; } } if ($this->id > 50) { //抛出自定义异常 throw new ExceedeMaxAllowedException('Max number of users is 50.'); } } public function getId():int { return $this->id; } //静态方法 public static function getLastId():int { return self::$lastId; } } ?>
test.php
function createBasicCustomer($id) { //捕获异常 try { echo "\nTrying to create a new customer.<br/>"; return new Basic($id, "name", "suname", "email"); } catch (InvalidIdException $e) { echo 'You cannot provide a negative id:' . $e->getMessage() . "<br/>"; } catch (ExceedeMaxAllowedException $e) { echo 'No more customer are allowed:' . $e->getMessage() . "<br/>"; } catch (Exception $e) { echo 'Unkown exception:' . $e->getMessage() . "<br/>"; } finally { echo "End of function.<br/>"; } } $basic = createBasicCustomer(1); $basic = createBasicCustomer(-1); $basic = createBasicCustomer(55);
输出:
Trying to create a new customer. End of function. Trying to create a new customer. You cannot provide a negative id:Id cannot be negative. End of function. Trying to create a new customer. No more customer are allowed:Max number of users is 50. End of function.
php中的Exception的更多相关文章
- .NET Core学习笔记(3)——async/await中的Exception处理
在写了很多年.NET程序之后,年长的猿类在面对异步编程时,仍不时会犯下致命错误,乃至被拖出去杀了祭天.本篇就async/await中的Exception处理进行讨论,为种族的繁衍生息做出贡献……处理a ...
- Oracle存储过程中异常Exception的捕捉和处理
Oracle存储过程中异常的捕捉和处理 CREATE OR REPLACE Procedure Proc_error_process ( v_IN in Varchar2, v_OUT Out Var ...
- Thinking in java中关于Exception的一道面试题.
今天看到Thinking in Java中一个关于Exception的例子:最后看到有一篇总结的比较好的文章, 这里拿来记录下, 文章地址是:http://blog.csdn.net/salerzha ...
- java 中的Exception RuntimeException 区别
在java的异常类体系中: 1.Error和RuntimeException是非检查型异常,其他的都是检查型异常; 2.所有方法都可以在不声明throws的情况下抛出RuntimeException及 ...
- Python中异常(Exception)的总结
Python中的异常处理 异常处理的语句结构 try: <statements> #运行try语句块,并试图捕获异常 except <name1>: <statement ...
- Oracle中的EXCEPTION
Oracle系统预定义的异常 比如:SELF_IS_NULL.VALUE_ERROR.ZERO_DIVIDE等Oracle中自带的异常类型 使用方法: DECLARE V_Result ); BEGI ...
- java中的exception stack有时候不输出的原因(转)
原文 https://www.cnblogs.com/lemonlotus/p/5650687.html 有时候,我们在看java错误日志时,只看到一个java.lang.NullPointerEx ...
- Abp中自定义Exception的HttpStatusCode
Abp中在新版本中,抛出的异常(比如:UserFriendlyException)通过AjaxResponse封装后返回的时候,HttpStatusCode默认指定成了500. 对于一些默认封装好的处 ...
- java中的exception stack有时候不输出的原因
有时候,我们在看java错误日志时,只看到一个java.lang.NullPointerException,却没有看到错误的栈,原因是启动时候有一项参数可以选择配置:OmitStackTraceInF ...
- GUI学习中错误Exception in thread "main" java.lang.NullPointerException
运行时出现错误:Exception in thread "main" java.lang.NullPointerException 该问题多半是由于用到的某个对象只进行了声明,而没 ...
随机推荐
- ros使用pppoe拨号获取ipv6,并且下发IPV6的dns到客户机win7
原文: https://www.mobile01.com/topicdetail.php?f=110&t=3405680 http://forum.mikrotik.com/viewtopic ...
- centos git编译
1. 下载git源码 https://git-scm.com 2. 根据文档一步步操作 https://git-scm.com/book/en/v2/Getting-Started-Installin ...
- Prometheus 介绍
我们知道zabbix在监控界占有不可撼动的地位,功能强大.但是对容器监控显得力不从心.为解决监控容器的问题,引入了prometheus技术.prometheus号称是下一代监控.接下来的文章打算围绕p ...
- CentOS 7搭建本地yum源和局域网yum源
这两天在部署公司的测试环境,在安装各种中间件的时候,发现各种依赖都没有:后来一检查,发现安装的操作系统是CentOS Mini版,好吧,我认了:为了完成测试环境的搭建,我就搭建了一个局域网的yum源. ...
- C# POST方式提交数据,接收方式,使用Request.Form[""]或Request[""]来获取
/// <summary> /// 调用接口 /// </summary> /// <param name="url"></param&g ...
- Proxy代理对象是如何调用invoke()方法的.
直奔主题,不说废话.先看java使用Proxy创建代理对象的代码. //一个开发者接口public interface Developer { String code(); void debug(); ...
- Kafka支持单集群20万分区
Kafka支持单集群20万分区 之前网上关于确定Kafka分区数的博客多多少少都源自于饶军大神的文章,如今他带来了这方面的第二篇文章,特此翻译一下,记录一下其中的要点. 原贴地址: https://w ...
- 二分法构造AVL树
public class ConvertSortedArrayToBinarySearchTree { public static TreeNode sortedArrayToBST(int[] nu ...
- html5点击input没有出现光标完美解决方案
html5点击input没有出现光标完美解决方案 <pre> <input type="text" placeholder="输入姓名" cl ...
- python入门之名称空间
name = 'tank' 什么是名称空间? 存放名字的空间 如果你想访问一个变量值,必须先访问对应的名称空间,拿到名字和对应的内存地址的绑定关系 名称空间的分类: 1.内置名称空间: python提 ...