当前位置:Gxlcms > PHP教程 > PHP的错误捕捉

PHP的错误捕捉

时间:2021-07-01 10:21:17 帮助过:35人阅读

PHP的异常捕捉

? ? ? 一直异或php的try catch 怎么使用

?

? ? ?看文档是说 需要new一个exception出来,才能catch到。

?

? ? ? 那如果需要捕捉到数组下限没找到 、或者方法传的参数不对 之类的 怎么办?

?

? ? ? 还好PHP提供了set_error_handler 这个函数,可以用户自己捕捉这些异常,比如:

  1. <!--?php
  2. set_error_handler("exception_error_handler");
  3. try{
  4. strpos();
  5. }catch(Exception $e){
  6. echo $e--->getMessage();
  7. }
  8. function exception_error_handler($errno, $errstr, $errfile, $errline ) {
  9. throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
  10. }

?

?

? ? ?这样就可以捕捉异常了

人气教程排行