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 ...
随机推荐
- Android_AsyncTask异步类
·AsyncTask是一个轻量级的异步抽象类 ·Android程序刚启动时,会同时启动一个像一个的主线程,这个主线程主要负责处理与UI有关的事件,有时也被称为UI线程,Android app中必须遵循 ...
- Maven的作用
Maven的作用 1.在开发中,为了保证编译通过,我们会到处去寻找jar包,当编译通过了,运行的时候,却发现"ClassNotFoundException",我们想到的是,难道还差 ...
- Spring Batch 批处理原则与建议
Spring Batch 批处理原则与建议 当我们构建一个批处理的过程时,必须注意以下原则: 通常情况下,批处理的过程对系统和架构的设计要够要求比较高,因此尽可能的使用通用架构来处理批量数据处理,降低 ...
- PP: Soft-DTW: a differentiable loss function for time-series
Problem: new loss Label: new loss; Abstract: A differentiable learning loss; Introduction: supervise ...
- 小白月赛22 A : 操作序列
A:操作序列 析题得说: 考察点 : 模拟,STL库容器的使用 坑点 : 区间不要搞丢东西 难点 : 这个题比较变态的是我们不知道每次输入每行是一个数还是两个数,就需要进行判断, 怎么判断呢?用 sc ...
- MySQL进阶之存储引擎MyISAM与InnoDB的区别
一.存储引擎(表类型) 通常意义上,数据库就是数据的集合,具体到计算机数据库可以是存储器上一些文件的集合或一些内存数据的集合.我们通常说的MySQL数据库.sql Server数据库等其实是数据库管理 ...
- JavaScript实现常见的数据结构
使用JavaScript实现栈.队列.链表.集合等常见数据结构.可能会有点用? 水 栈(Stack) 实际上JavaScript的Array本身就具有栈和队列的特性,所以我们可以借助Array来实现它 ...
- Python TCP与UDP的区别
TCP:英文全拼(Transmission Control Protocol)简称传输控制协议,它是一种面向连接的.可靠的.基于字节流的传输层通信协议. TCP通信需要经过创建连接.数据传送.终止连接 ...
- Loj515 「LibreOJ β Round #2」贪心只能过样例 - Bitset,Dp
bitset的基本应用了 类似可行性背包的dp考虑 复杂度O(nmL/64) #include <bits/stdc++.h> using namespace std; bitset &l ...
- springboot 扫描不到包 @SpringBootApplication 自动配置原理
解决方案 在main类中增加注解 @ComponentScan("com.test.test.*") 扫描具体的包 @ComponentScan(basePackages = {& ...