1.使用的关键字

try:检测代码
catch:发现异常
throw:抛出异常信息
finally:无论有无异常都会执行 可以省掉
Exception:异常类
下面是php手册中的
image.png

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>无标题文档</title>
  6. </head>
  7. <body>
  8. <?php
  9. if(!empty($_POST['button'])){
  10. try{
  11. $ceshi=$_POST['test'];
  12. if($ceshi=='')
  13. throw new Exception('神马,不说话就是默认我是帅比了',1001);
  14. if($ceshi=='不帅')
  15. throw new Exception("错误,你撒谎!", 1002);
  16. echo '是的 0ne最帅了!~!';
  17. }catch(Exception $ex){
  18. echo '错误信息:'.$ex->getMessage(),'<br>';
  19. echo '错误码:'.$ex->getCode(),'<br>';
  20. echo '文件地址:'.$ex->getFile(),'<br>';
  21. echo '错误行号:'.$ex->getLine(),'<br>';
  22. }
  23. finally{
  24. }
  25. }
  26. ?>
  27. <form method="post" action="">
  28. 0ne帅不帅: <input type="text" name="test"> <br />
  29. <input type="submit" name="button" value="提交">
  30. </form>
  31. </body>
  32. </html>

image.png