没有定义命名空间的情况下 , 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. Android studio中为项目添加模块依赖的过程

    https://blog.csdn.net/cheng__lu/article/details/74574582 Android studio中为项目添加模块依赖的过程 1.点击菜单file>p ...

  2. 微信小程序调起支付API

    官方文档: https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_7 https://developers.weixin.q ...

  3. 快速搭建一个基于react的项目

    最近在学习react,快速搭建一个基于react的项目 1.创建一个放项目文件夹,用编辑器打开 2.打开集成终端输入命令: npm install -g create-react-app 3. cre ...

  4. ansible-七种武器

    1. ansible命令 2. ansible-doc是ansible模块说明文档,针对每个模块都有详细用法说明以及应用案例介绍 3. ansible-console是ansible为用户提供的一款交 ...

  5. Spring Boot整合Freemarker

    一.首先导入依赖 <!-- 添加freemarker模版的依赖 --> <dependency>     <groupId>org.springframework. ...

  6. .net Core 安装在linux上

    1.安装 .net Core 参考官方网站 https://dotnet.microsoft.com/learn/dotnet/hello-world-tutorial/install 2.发布应用程 ...

  7. c语言 printf格式化输出

    #include <iostream> #include<stdio.h> #include <cstring> using namespace std; int ...

  8. 【转载】JS导出CSV文件

    转自:http://www.cnblogs.com/dengnan/p/3990211.html 通过自己实际测试有以下几种方法 方法一通过a标签实现,把要导出的数据用“\n”和“,”拼接成一个字符串 ...

  9. c++ 踩坑大法好 复合数据类型------vector

    1,vector是啥? 是具有动态大小的数组,具有顺序.能够存放各种类型的对象.相比于固定长度的数组,运行效率稍微低一些,不过很方便. 2,咋用? 声明: vector <int> vi; ...

  10. 逗号运算符与括号 - C语言

    例1 int x; int a=(x=2),12;// 赋值优先级高于逗号,相当于a=x=2,12是多余的 printf("a=%d",a); 结果:a=2 例2 int x; i ...