php抛出异常Exception和\Exception使用区别
没有定义命名空间的情况下 , Exception和\Exception 均可正常执行抛出异常;
定义命名空间的情况 , Exception 会在定义的命名空间下找对应的异常类 , 如果没有定义异常类 , 则会报错 ;
定义命名空间的情况 , \Exception 会按照php默认的异常类执行抛出异常 ;
建议: 抛出异常 使用 \Exception !
#1: 没有定义命名空间 使用Exception
<?php
try {
throw new Exception("抛出异常");
} catch (Exception $e) {
echo '捕获到异常'.$e->getMessage();
}
#1> 执行结果
捕获到异常抛出异常
#2: 没有定义命名空间 使用\Exception
<?php
try {
throw new \Exception("抛出异常");
} catch (\Exception $e) {
echo '捕获到异常'.$e->getMessage();
}
#2> 执行结果
捕获到异常抛出异常
#3: 有定义命名空间 使用Exception
<?php
namespace Test; try {
throw new Exception("抛出异常");
} catch (Exception $e) {
echo '捕获到异常'.$e->getMessage();
}
#3> 执行结果
Fatal error: Uncaught Error: Class 'Anxiaojing\Exception' not found *** Stack trace: #0 {main} thrown in ***
#4: 有定义命名空间 使用\Exception
<?php
namespace Test; try {
throw new \Exception("抛出异常");
} catch (\Exception $e) {
echo '捕获到异常'.$e->getMessage();
}
#4> 执行结果
捕获到异常抛出异常
php抛出异常Exception和\Exception使用区别的更多相关文章
- EXCEPTION与ERROR的区别
EXCEPTION与ERROR的区别
- Checked Exception & Unchecked Exception
查Spring事务管理时看到一句话: Spring使用声明式事务处理,默认情况下,如果被注解的数据库操作方法中发生了unchecked异常,所有的数据库操作将rollback:如果发生的异常是chec ...
- Method threw 'org.hibernate.exception.SQLGrammarException' exception. Cannot evaluate com.hotel.Object_$$_jvst485_15.toString()
数据库字段和类Object属性不匹配,Method threw 'org.hibernate.exception.SQLGrammarException' exception. Cannot eval ...
- ?--Porg.springframework.beans.MethodInvocationException: Property 'username' threw exception; nested exception is java.lang.NullPointerException
使用BoneCP作为连接池,在启动Tomcat报出以下异常: 一月 02, 2016 2:12:17 下午 org.apache.tomcat.util.digester.SetPropertiesR ...
- 解决Redisson出现Failed to instantiate [org.redisson.api.RedissonClient]: Factory method 'create' threw exception; nested exception is java.lang.ArrayIndexOutOfBoundsException: 0的问题
一.背景 最近项目中使用了redisson的哨兵模式来作为redis操作的客户端,然后一个意外出现了,启动报:Failed to instantiate [org.redisson.api.Redis ...
- org.springframework.beans.MethodInvocationException: Property 'cacheManager' threw exception; nested exception is org.apache.shiro.cache.CacheException: net.sf.ehcache.CacheException: Caches cannot be
shiro cache manage配置报错: org.springframework.beans.MethodInvocationException: Property 'cacheManager' ...
- method 'redisConnectionFactory' threw exception; nested exception is java.lang.NoClassDefFoundError
spring boot 整合redis是报了如下错误 org.springframework.beans.factory.UnsatisfiedDependencyException: Error c ...
- spring和mybatis整合报错:org.springframework.beans.MethodInvocationException: Property 'dataSource' threw exception; nested exception is java.lang.NoClassDefFoundError
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyExceptio ...
- During handling of the above exception, another exception occurred:
今天在计算机矩阵相关性,准备删除相关性高的列中,出现了这样的问题: During handling of the above exception, another exception occurred ...
随机推荐
- Grammar Framework 1
Word is cheap, show me the grammar. Doc requirements:TODO 1. English doc 2. Mark the importance 语法框架 ...
- Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache的类型初始值设定项引发异常。 ---> System.IO.FileLoadException: 未能加载文件或程序集
场景: 安装程序到全新的环境的电脑时中(此时已经安装了能正常安装程序电脑的环境) 完整错误: Application_ThreadException:System.TypeInitialization ...
- mysql 视图、触发器、事务、存储过程、函数
一 视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的 ...
- 0级搭建类012-Windows Server 2019安装(2019) 公开
项目文档引子系列是根据项目原型,制作的测试实验文档,目的是为了提升项目过程中的实际动手能力,打造精品文档AskScuti. 项目文档引子系列目前不对外发布,仅作为博客记录.如学员在实际工作过程中需提前 ...
- i春秋公益赛 ezpload
题目思路:一看解出的人比较多,emmm,传个马,命令执行一下.最后读到flag文件. /readflag,可执行. 题对萌新比较友好...... 考点:Linux命令,文件上传,命令执行. http: ...
- [转]从实例谈OOP、工厂模式和重构
有了翅膀才能飞,欠缺灵活的代码就象冻坏了翅膀的鸟儿.不能飞翔,就少了几许灵动的气韵.我们需要给代码带去温暖的阳光,让僵冷的翅膀重新飞起来.结合实例,通过应用OOP.设计模式和重构,你会看到代码是怎样一 ...
- openWRT和LuCI
openwrt是一套集成在板子上的系统,通过ip进入到其页面上 Luci是lua和UCI统一配置接口的合体,实现路由的网页配置界面(相当于一个前端框架)
- ajax从jsp向servlet传值
function Delete(s){ xmlHttp=new XMLHttpRequest(); var url = "/emp/FindSpecial?selcol=" +s; ...
- createElement(九)
Vue.js 利用 createElement 方法创建 VNode,它定义在 src/core/vdom/create-elemenet.js 中: // wrapper function for ...
- for循环与闭包
Es5 function box(){ var arr = []; ;i<;i++){ arr[i] = (function(num){ //自我执行,并传参(将匿名函数形成一个表达式)(传递一 ...