You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.0 KiB

  1. <?php
  2. class emailException extends Exception{
  3. function __toString()
  4. {
  5. return "email is null ".$this->getFile().' '.$this->getLine().'<br>';
  6. }
  7. }
  8. class nameException extends Exception{}
  9. function reg($reg){
  10. if (empty($reg['email'])) { //如果数组reg里键email的值是空的
  11. throw new emailException("email is null",1);//2.抛出异常后推出了函数
  12. }
  13. echo "-----".'<br>'; //代码没有执行到这里
  14. if (empty($reg['name'])) {
  15. throw new nameException('name is null',2);
  16. }
  17. echo "-----".'<br>';
  18. }
  19. try {
  20. $reg = array('phone'=>'12344544');
  21. reg($reg); //1.传入的数组里没有eamil和name
  22. } catch (emailException $e) { //3.捕获的是email is null这个异常
  23. echo $e;
  24. } catch (nameException $e) { //代码没有进入这个catch
  25. echo $e->getMessage().'<br>';
  26. echo $e->getCode().'<br>';
  27. } finally { //4.最后进入finally
  28. echo "finally";
  29. }
  30. ?>