zoukankan      html  css  js  c++  java
  • 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 'AnxiaojingException' not found *** Stack trace: #0 {main} thrown in ***

    #4: 有定义命名空间 使用Exception

    <?php
    namespace Test;
    
    try {
      throw new Exception("抛出异常");
    } catch (Exception $e) {
      echo '捕获到异常'.$e->getMessage();
    }

    #4> 执行结果
    捕获到异常抛出异常

  • 相关阅读:
    ZOJ2913Bus Pass(BFS+set)
    HDU1242 Rescue(BFS+优先队列)
    转(havel 算法)
    ZOJ3761(并查集+树的遍历)
    ZOJ3578(Matrix)
    HDU1505
    ZOJ3574(归并排序求逆数对)
    VUE-脚手架搭建
    VUE脚手架搭建
    VUE-node.js
  • 原文地址:https://www.cnblogs.com/niuben/p/10855282.html
Copyright © 2011-2022 走看看