当前位置:  首页>> 技术小册>> PHP合辑2-高级进阶

异常是程序本身可以处理的意外程序结果。PHP中的异常处理与所有编程语言中的异常处理几乎类似。

PHP为此提供了以下专门的关键字。

  • try:它表示可能发生异常的代码块。
  • catch:它表示在抛出特定异常时要执行的代码块。
  • throw:它用于抛出异常。它也用于列出函数抛出但不处理的异常。
  • finally:它用于代替catch块或catch块之后,基本上它是用于PHP代码的清理活动。
  • 为什么要在PHP中进行异常处理?

异常处理相对于错误处理有以下主要优点:

错误处理代码与普通代码分离:在传统的错误处理代码中,总是有一个if-else块来处理错误。这些条件和处理错误的代码混在一起,导致代码难以阅读。使用try-catch块,代码变得易于阅读。

错误类型的分组:在PHP中,基本类型和对象都可以作为异常抛出。它可以创建异常对象的层次结构,在命名空间或类中分组异常,并根据类型对它们进行分类。

示例:

  1. <?php
  2. // PHP Program to illustrate normal
  3. // try catch block code
  4. function demo($var) {
  5. echo " Before try block";
  6. try {
  7. echo "\n Inside try block";
  8. // If var is zero then only if will be executed
  9. if($var == 0)
  10. {
  11. // If var is zero then only exception is thrown
  12. throw new Exception('Number is zero.');
  13. // This line will never be executed
  14. echo "\n After throw (It will never be executed)";
  15. }
  16. }
  17. // Catch block will be executed only
  18. // When Exception has been thrown by try block
  19. catch(Exception $e) {
  20. echo "\n Exception Caught", $e->getMessage();
  21. }
  22. // This line will be executed whether
  23. // Exception has been thrown or not
  24. echo "\n After catch (will be always executed)";
  25. }
  26. // Exception will not be raised
  27. demo(5);
  28. // Exception will be raised here
  29. demo(0);
  30. ?>

output

  1. Before try block
  2. Inside try block
  3. After catch (will be always executed)
  4. Before try block
  5. Inside try block
  6. Exception CaughtNumber is zero.
  7. After catch (will be always executed)

示例2

  1. <?php
  2. // PHP Program to illustrate normal
  3. // try catch block code
  4. function demo($var) {
  5. echo " Before try block";
  6. try {
  7. echo "\n Inside try block";
  8. // If var is zerothen only if will be executed
  9. if($var == 0) {
  10. // If var is zero then only exception is thrown
  11. throw new Exception('Number is zero.');
  12. // This line will never be executed
  13. echo "\n After throw it will never be executed";
  14. }
  15. }
  16. // Catch block will be executed only
  17. // When Exception has been thrown by try block
  18. catch(Exception $e) {
  19. echo "\n Exception Caught" . $e->getMessage();
  20. }
  21. finally {
  22. echo "\n Here cleanup activity will be done";
  23. }
  24. // This line will be executed whether
  25. // Exception has been thrown or not
  26. echo "\n After catch it will be always executed";
  27. }
  28. // Exception will not be raised
  29. demo(5);
  30. // Exception will be raised here
  31. demo(0);
  32. ?>

output

  1. Before try block
  2. Inside try block
  3. Here cleanup activity will be done
  4. After catch (will be always executed)
  5. Before try block
  6. Inside try block
  7. Exception CaughtNumber is zero.
  8. Here cleanup activity will be done
  9. After catch (will be always executed)

使用自定义异常类

  1. <?php
  2. class myException extends Exception {
  3. function get_Message() {
  4. // Error message
  5. $errorMsg = 'Error on line '.$this->getLine().
  6. ' in '.$this->getFile()
  7. .$this->getMessage().' is number zero';
  8. return $errorMsg;
  9. }
  10. }
  11. function demo($a) {
  12. try {
  13. // Check if
  14. if($a == 0) {
  15. throw new myException($a);
  16. }
  17. }
  18. catch (myException $e) {
  19. // Display custom message
  20. echo $e->get_Message();
  21. }
  22. }
  23. // This will not generate any exception
  24. demo(5);
  25. // It will cause an exception
  26. demo(0);
  27. ?>

output:

  1. Error on line 20 in /home/45ae8dc582d50df2790517e912980806.php0 is number zero

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

示例:

  1. <?php
  2. // PHP Program to illustrate normal
  3. // try catch block code
  4. // Function for Uncaught Exception
  5. function myException($exception) {
  6. // Details of Uncaught Exception
  7. echo "\nException: " . $exception->getMessage();
  8. }
  9. // Set Uncaught Exception handler
  10. set_exception_handler('myException');
  11. function demo($var) {
  12. echo " Before try block";
  13. try {
  14. echo "\n Inside try block";
  15. // If var is zero then only if will be executed
  16. if($var == 0)
  17. {
  18. // If var is zero then only exception is thrown
  19. throw new Exception('Number is zero.');
  20. // This line will never be executed
  21. echo "\n After throw (it will never be executed)";
  22. }
  23. }
  24. // Catch block will be executed only
  25. // When Exception has been thrown by try block
  26. catch(Exception $e) {
  27. echo "\n Exception Caught", $e->getMessage();
  28. }
  29. // This line will be executed whether
  30. // Exception has been thrown or not
  31. echo "\n After catch (will be always executed)";
  32. if($var < 0) {
  33. // Uncaught Exception
  34. throw new Exception('Uncaught Exception occurred');
  35. }
  36. }
  37. // Exception will not be raised
  38. demo(5);
  39. // Exception will be raised here
  40. demo(0);
  41. // Uncaught Exception
  42. demo (-3);
  43. ?>

output:

  1. Before try block
  2. Inside try block
  3. After catch (will be always executed)
  4. Before try block
  5. Inside try block
  6. Exception CaughtNumber is zero.
  7. After catch (will be always executed)
  8. Before try block
  9. Inside try block
  10. After catch (will be always executed)
  11. Exception: Uncaught Exception occurred