没有定义命名空间的情况下 , 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. 统一操作系统 UOS 回应质疑 (独立思考)

    统一操作系统 UOS 回应质疑 自从统一操作系统UOS正式版面向合作伙伴发布之后,小编就关注到网上对统一操作系统UOS众说纷纭.留意了近期网友们发表的评论: “又多了一个骗钱的国产操作系统” “从UO ...

  2. vsftpd最详细的配置文件

    vsftpd作为一个主打安全的FTP服务器,有很多的选项设置.下面介绍了vsftpd的配置文件列表,而所有的配置都是基于vsftpd.conf这个配置文件的.本文将提供完整的vsftpd.conf的中 ...

  3. python 3 可迭代对象与迭代器

    1,可迭代对象 内部含有__iter__方法的对象是可迭代对象 遵循可迭代协议 dir() 检查对象含有什么方法 dir()会返回一个列表,这个列表中含有该对象的以字符串的形式所有方法名.这样我们就可 ...

  4. 【vue 权威指南】 学习笔记 一

    内容简介 vue.js 是一个用来开发Web界面的前端库. 1.vue.js 是什么 vue.js 是一个构建数据驱动的web界面的库,vue.js 通过简单的API提供高效的数据绑定和灵活的组件系统 ...

  5. MPI Maelstrom POJ - 1502 floyd

    #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> usin ...

  6. 栈和队列----设计一个有getMin功能的栈

    设计一个有getMin功能的栈 设计一个具有getMin功能的栈,可以返回栈中的最小的元素,可以使用现有的栈的数据结构,要求pop/push/getMin操作的时间复杂度是O(1). package ...

  7. 402 WebEx会议教程二 —— 召开会议

    ··· WebEx会议教程二 —— 召开会议 简介:召开一个WebEx会议,并邮件邀请其他人参加会议 1.  安装快捷会议工具,将WebEx按钮集成到Outlook中.    WebEx快捷会议工具- ...

  8. idea软件操作

    1.快捷键: 1.1.格式化代码:crtl+alt+L 1.2.一些构造啊,setter/getter等方法:alt+insert 1.3.crtl + f 搜素当前页面

  9. <input type="file">文件上传

    <input> type 类型为 file 时使得用户可以选择一个或多个元素以提交表单的方式上传到服务器上,或者通过 Javascript 的 File API 对文件进行操作 . 常用i ...

  10. 天兔修改登录页的title

    1.将 /opt/lampp/htdocs/lepus/application/views/login.php 文件中 第6行 <title><?php echo $this-> ...