没有定义命名空间的情况下 , 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. 安装AB到CentOS(YUM)

    运行环境 系统版本:CentOS Linux release 7.3.1611 (Core) 软件版本:httpd-tools-2.4.6 硬件要求:无 安装过程 1.安装YUM-EPEL源 HTTP ...

  2. 在什么情况下,不写notify()或者notifyAll()就能唤醒被wait()阻塞的线程?

    之前再看java关于线程的某视频时,发现在JDK源码中,join()=join(0)=wait()=wait(0),但是视频中在join()了之后,并没有用notify()或者notifyAll()去 ...

  3. Android View框架的draw机制

    概述 Android中View框架的工作机制中,主要有三个过程: 1.View树的测量(measure) Android View框架的measure机制 2.View树的布局(layout)Andr ...

  4. Django生成脚本迁移文件时,报错django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.

    一.本人环境:django:3.0.2, python:3.8.1,  pymysql:0.9.3 二.解决步骤: 1.django目录下找到 base.py文件: 2.在base.py文件中注释以下 ...

  5. Java Day2(下)

    Java learning_Day2(下) 本人学习视频用的是马士兵的,也在这里献上 <链接:https://pan.baidu.com/s/1qKNGJNh0GgvlJnitTJGqgA> ...

  6. 《深入理解java虚拟机》读书笔记三——第四章

    第四章 虚拟机性能监控与故障处理工具 1.JDK命令行工具 jps命令: 作用:列出正在运行的虚拟机进程. 格式:jps [option] [hostid] 选项:-q 只输出LVMID(Local ...

  7. 为data中的某一个对象添加一个属性不起作用——this.$set的正确使用

    this.$set(obj, key, value) 我们在项目开发的过程中,经常会遇到这种情况:为data中的某一个对象添加一个属性 <template> <div class=& ...

  8. linux - 从网上下载文件到服务器

    命令格式 wget URL地址 实例 wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.33-linux-glibc2.5-x86 ...

  9. SimpleDateFormat中YYYYmmDDhhMMss大小写问题-获取不到正确时间以及常见的格式串

    1.问题解决: SimpleDateFormat sf = new SimpleDateFormat("YYYYmmDDhhMMss");String transTime = &q ...

  10. Mysql单实例数据库安装

    第1章 MySQL数据库安装 在当今的大中型互联网企业里,MySQL数据库服务几乎都是运行在Linux系列操作系统上,当然,你在可以运行在Windows/Unix等商业操作系统上,大中型互联网企业使用 ...