PHP中的异常处理超级指南

异常是程序本身可以处理的意外程序结果。 PHP中的异常处理几乎类似于所有编程语言中的异常处理。
PHP为此提供了以下专用关键字。

  • 尝试:它代表其中可能发生异常的代码块。
  • 抓住:它表示当引发特定异常时将执行的代码块。
  • 扔:它用于引发异常。它还用于列出函数引发但无法自行处理的异常。
  • 最后:它用于代替catch块或在catch块之后使用, 基本上用于PHP代码中的清理活动。
为什么在PHP中进行异常处理?
以下是异常处理相对于错误处理的主要优点
  • 错误处理代码与普通代码的分离:在传统的错误处理代码中, 总是存在else块来处理错误。这些条件和处理错误的代码混杂在一起, 因此变得不可读。使用try捕获块代码变得可读。
  • 错误类型分组:在PHP中, 基本类型和对象都可以作为异常抛出。它可以创建异常对象的层次结构, 在名称空间或类中对异常进行分组, 并根据类型对它们进行分类。
PHP中的异常处理:
  • 以下代码说明了PHP中常规try catch块的流??程: filter_none
    编辑

    play_arrow
    链接
    亮度_4
    代码



    < ?php     // PHP Program to illustrate normal // try catch block code function demo( $var ) {         echo " Before try block" ;         try {                 echo "\n Inside try block" ;                                             // If var is zero then only if will be executed                 if ( $var == 0)                 {                                                             // If var is zero then only exception is thrown                         throw new Exception( 'Number is zero.' );                                                             // This line will never be executed                         echo "\n After throw (It will never be executed)" ;                 }         }                             // Catch block will be executed only          // When Exception has been thrown by try block         catch (Exception $e ) {                         echo "\n Exception Caught" , $e -> getMessage();                 }                                     // This line will be executed whether                 // Exception has been thrown or not                  echo "\n After catch (will be always executed)" ; }     // Exception will not be rised demo(5);     // Exception will be rised here demo(0); ?>

    chevron_right


    filter_none



    输出如下:
    Before try block Inside try block After catch (will be always executed) Before try block Inside try block Exception CaughtNumber is zero. After catch (will be always executed)

  • 以下代码说明了正常的try catch和finally块在PHP中的流程 filter_none
    编辑

    play_arrow
    链接
    亮度_4
    代码



    < ?php     // PHP Program to illustrate normal // try catch block code function demo( $var ) {                     echo " Before try block" ;         try {                 echo "\n Inside try block" ;                                             // If var is zerothen only if will be executed                 if ( $var == 0) {                                                             // If var is zero then only exception is thrown                         throw new Exception( 'Number is zero.' );                                                             // This line will never be executed                         echo "\n After throw it will never be executed" ;                 }         }                             // Catch block will be executed only          // When Exception has been thrown by try block         catch (Exception $e ) {                 echo "\n Exception Caught" . $e -> getMessage();         }         finally {                 echo "\n Here cleanup activity will be done" ;         }                             // This line will be executed whether         // Exception has been thrown or not          echo "\n After catch it will be always executed" ; }     // Exception will not be rised demo(5);     // Exception will be rised here demo(0); ?>

    chevron_right


    filter_none



    输出如下:
    Before try block Inside try block Here cleanup activity will be done After catch (will be always executed) Before try block Inside try block Exception CaughtNumber is zero. Here cleanup activity will be done After catch (will be always executed)

  • 使用自定义异常类 filter_none
    编辑

    play_arrow
    链接
    亮度_4
    代码



    < ?php class myException extends Exception {         function get_Message() {                                     // Error message                 $errorMsg = 'Error on line ' . $this -> getLine().                                         ' in ' . $this -> getFile()                 . $this -> getMessage(). ' is number zero' ;                 return $errorMsg ;         } }     function demo( $a ) {         try {                             // Check if                 if ( $a == 0) {                         throw new myException( $a );                 }         }                     catch (myException $e ) {                             // Display custom message                 echo $e -> get_Message();         } }     // This will not generate any exception demo(5);     // It will cause an exception demo(0); ?>  

    chevron_right


    filter_none



    输出如下:
    Error on line 20 in /home/45ae8dc582d50df2790517e912980806.php0 is number zero

  • 设置顶级异常处理程序:set_exception_handler()函数将所有用户定义的函数设置为所有未捕获的异常。 filter_none
    编辑

    play_arrow
    链接
    亮度_4
    代码



    < ?php     // PHP Program to illustrate normal // try catch block code     // Function for Uncaught Exception function myException( $exception ) {                     // Details of Uncaught Exception         echo "\nException: " . $exception -> getMessage(); }     // Set Uncaught Exception handler set_exception_handler( 'myException' ); function demo( $var ) {                     echo " Before try block" ;         try {                 echo "\n Inside try block" ;                                             // If var is zero then only if will be executed                 if ( $var == 0)                 {                                                             // If var is zero then only exception is thrown                         throw new Exception( 'Number is zero.' );                                                             // This line will never be executed                         echo "\n After throw (it will never be executed)" ;                 }         }                             // Catch block will be executed only          // When Exception has been thrown by try block         catch (Exception $e ) {                 echo "\n Exception Caught" , $e -> getMessage();         }                             // This line will be executed whether         // Exception has been thrown or not          echo "\n After catch (will be always executed)" ;                             if ( $var < 0) {                                      // Uncaught Exception                 throw new Exception( 'Uncaught Exception occurred' );         } }     // Exception will not be rised demo(5);     // Exception will be rised here demo(0);     // Uncaught Exception demo (-3); ?>

    chevron_right


    filter_none



    输出如下: 【PHP中的异常处理超级指南】
    Before try block Inside try block After catch (will be always executed) Before try block Inside try block Exception CaughtNumber is zero. After catch (will be always executed) Before try block Inside try block After catch (will be always executed) Exception: Uncaught Exception occurred

    推荐阅读