没有定义命名空间的情况下 , 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使用区别的更多相关文章

  1. EXCEPTION与ERROR的区别

    EXCEPTION与ERROR的区别

  2. Checked Exception & Unchecked Exception

    查Spring事务管理时看到一句话: Spring使用声明式事务处理,默认情况下,如果被注解的数据库操作方法中发生了unchecked异常,所有的数据库操作将rollback:如果发生的异常是chec ...

  3. 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 ...

  4. ?--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 ...

  5. 解决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 ...

  6. 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' ...

  7. method 'redisConnectionFactory' threw exception; nested exception is java.lang.NoClassDefFoundError

    spring boot 整合redis是报了如下错误 org.springframework.beans.factory.UnsatisfiedDependencyException: Error c ...

  8. 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 ...

  9. During handling of the above exception, another exception occurred:

    今天在计算机矩阵相关性,准备删除相关性高的列中,出现了这样的问题: During handling of the above exception, another exception occurred ...

随机推荐

  1. Linux的语系的修改

    1. 显示目前所支持的语系 [root@test ~]# echo $LANG 2. 修改语系成为英文语系 字体集目录:"/etc/sysconfig/i18n",编辑该文件,将字 ...

  2. geoserver发布地图服务

    1.      Geoserver启动 blog.csdn.net 2014-09-18 20:30 Geoserver是著名的开源GIS软件之一.也是项目中常用的地图服务软件.基于geoserver ...

  3. 跳跃【BFS】

    From 牛客网:https://ac.nowcoder.com/acm/problem/25160

  4. 使用mysql8.+版本,使用mybatis的代码生成工具:mybatis-generator连接数据库时Unknown initial character set index '255' received from server. Initial client character set can be forced via the 'characterEncoding' property.

    Error connecting to database: (using class org.gjt.mm.mysql.Driver)Unknown initial character set ind ...

  5. java课后动手动脑作业

    public class Suiji { public long a=12345L;//定义long类型的a,b,c变量 public long c=12345L; public long m=456 ...

  6. OCM 12c | OCM 12c Update | OCM 11g (Retiring Dec 31, 2019) | OCM 11g考试延期至2020.04.30

     OCM 全球考试安排时间表 View A Worldwide OCM Schedule Oracle Database 12c Certified Master Exam (OCM) OCM 12c ...

  7. .Net Core的总结

    一.什么是.NET Core .NET Core是一个开源通用的开发框架,支持跨平台,即支持在Window,macOS,Linux等系统上的开发和部署,并且可以在硬件设备,云服务,和嵌入式/物联网方案 ...

  8. JS中axios使用注意点

    今天遇到这样一个问题,前端会同时弹出成功和失败的两个提示框,由于不是本人操作,也没有怀疑是前端代码的问题,就索性根据后端的日志作为分析依据,开始个人以为是后端接口上班了两次结果,一个是成功,另外一个是 ...

  9. 压缩软件推荐(bandizip)

    提及 Windows 平台的压缩软件,大家往往想起老牌的 WinRAR.开源免费的 7-Zip.国产的快压.好压.360 压缩之类,甚至还有时代的眼泪 WinZip.一直以来,压缩软件因为作为十分基础 ...

  10. pytest参数化 parametrize

    pytest.mark.parametrize装饰器可以实现测试用例参数化 parametrizing 1.这里是一个实现检查一定的输入和期望输出测试功能的典型例子 # content of test ...