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 ...
随机推荐
- Linux X_window与文本模式的切换
用x_window启动的情况下的切换方法: [Ctrl] + [Alt] + [F1] ~ [F6] :文字接口登陆 tty1 ~ tty6 终端机: [Ctrl] + [Alt] + [F7] ...
- LeetCode 1046. 最后一块石头的重量 (贪心)
有一堆石头,每块石头的重量都是正整数. 每一回合,从中选出两块最重的石头,然后将它们一起粉碎.假设石头的重量分别为 x 和 y,且 x <= y.那么粉碎的可能结果如下: 如果 x == y,那 ...
- HTML5,从零开始
一.网页的组成部分 <!DOCTYPE html> <html> <head> <title>这是标题</title> <meta c ...
- 1级搭建类108-Oracle 11gR2 SI FS(Windows Server 2019)公开
Oracle 11gR2 单实例文件系统在Windows Server 2019上的安装 在线查看
- 曼孚科技:AI自然语言处理(NLP)领域常用的16个术语
自然语言处理(NLP)是人工智能领域一个十分重要的研究方向.NLP研究的是实现人与计算机之间用自然语言进行有效沟通的各种理论与方法. 本文整理了NLP领域常用的16个术语,希望可以帮助大家更好地理解 ...
- Java枚举类型的使用,数值的二进制表示
一.Java枚举类型的使用 首先请看这段代码: package java上课; public class EnumTest { public static void main(String[] arg ...
- Codeforces 764C Timofey and a tree
Each New Year Timofey and his friends cut down a tree of n vertices and bring it home. After that th ...
- python创建字典的三种方式
创建空字典: dict_eq={} print(type(dict)) 直接赋值创建字典: dict_eq={'a':1,'b':2,'c':'adbc'} 通过关键字dict和关键字参数创建 dic ...
- linux 6.9 补丁修补漏洞
1 先将openssh-8.0p1.tar.gz 上传到 root下的/opt 文件夹下 解压 tar -zxvf openssh-8.0p1.tar.gz -C /opt 2 启动vncserv ...
- js判断Android和Ios
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" ...