异常是程序本身可以处理的意外程序结果。 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 codefunction demo( $var ) {echo " Before try block" ;try {echo "\n Inside try block" ;// If var is zero then only if will be executedif ( $var == 0){// If var is zero then only exception is thrownthrow new Exception( 'Number is zero.' );// This line will never be executedecho "\n After throw (It will never be executed)" ;}}// Catch block will be executed only// When Exception has been thrown by try blockcatch (Exception $e ) {echo "\n Exception Caught" , $e ->getMessage();}// This line will be executed whether// Exception has been thrown or notecho "\n After catch (will be always executed)" ;}// Exception will not be riseddemo(5);// Exception will be rised heredemo(0);?>chevron_rightfilter_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 codefunction demo( $var ) {echo " Before try block" ;try {echo "\n Inside try block" ;// If var is zerothen only if will be executedif ( $var == 0) {// If var is zero then only exception is thrownthrow new Exception( 'Number is zero.' );// This line will never be executedecho "\n After throw it will never be executed" ;}}// Catch block will be executed only// When Exception has been thrown by try blockcatch (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 notecho "\n After catch it will be always executed" ;}// Exception will not be riseddemo(5);// Exception will be rised heredemo(0);?>chevron_rightfilter_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
代码<?phpclass 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 ifif ( $a == 0) {throw new myException( $a );}}catch (myException $e ) {// Display custom messageecho $e ->get_Message();}}// This will not generate any exceptiondemo(5);// It will cause an exceptiondemo(0);?>chevron_rightfilter_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 Exceptionfunction myException( $exception ) {// Details of Uncaught Exceptionecho "\nException: " . $exception ->getMessage();}// Set Uncaught Exception handlerset_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 executedif ( $var == 0){// If var is zero then only exception is thrownthrow new Exception( 'Number is zero.' );// This line will never be executedecho "\n After throw (it will never be executed)" ;}}// Catch block will be executed only// When Exception has been thrown by try blockcatch (Exception $e ) {echo "\n Exception Caught" , $e ->getMessage();}// This line will be executed whether// Exception has been thrown or notecho "\n After catch (will be always executed)" ;if ( $var < 0) {// Uncaught Exceptionthrow new Exception( 'Uncaught Exception occurred' );}}// Exception will not be riseddemo(5);// Exception will be rised heredemo(0);// Uncaught Exceptiondemo (-3);?>chevron_rightfilter_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) Before try block Inside try block After catch (will be always executed) Exception: Uncaught Exception occurred