PHP的异常处理
异常是可以由程序本身处理的意外程序结果。 PHP的异常处理几乎类似于所有编程语言中的异常处理。
为此, PHP提供了以下专用关键字。
- try:它代表可能出现异常的代码块。
- catch:它表示在抛出特定异常时将执行的代码块。
- throw:用于抛出异常。它还用于列出函数抛出但不处理自身的异常。
- finally:它用于代替 catch 块或在 catch 块之后基本上用于PHP代码中的清理活动。
为什么要在PHP异常处理?
以下是异常处理相对于错误处理的主要优点
- 错误处理代码与普通代码的分离:在传统的错误处理代码中,总是有 if else 块来处理错误。这些条件和处理错误的代码混合在一起,因此变得不可读。使用 try Catch 块代码变得可读。
- 错误类型分组:在PHP,基本类型和对象都可以作为异常抛出。它可以创建异常对象的层次结构,在命名空间或类中对异常进行分组,根据类型对它们进行分类。
PHP的异常处理:
- 以下代码解释了PHP中正常 try catch 块的流程:
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); ?>
输出: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)
- 以下代码解释了PHP中正常 try catch 和 finally 块的流程
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); ?>
输出: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)
- 使用自定义异常类
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); ?>
输出:Error on line 20 in /home/45ae8dc582d50df2790517e912980806.php0 is number zero
- 设置顶级异常处理程序: set_exception_handler()函数将所有用户定义的函数设置为所有未捕获的异常。
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); ?>
输出: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