jiaxp 发表于 2017-3-22 09:50:10

PHP的异常捕捉

  一直异或php的try catch 怎么使用
  看文档是说 需要new一个exception出来,才能catch到。
  那如果需要捕捉到数组下限没找到 、或者方法传的参数不对 之类的 怎么办?
  还好PHP提供了set_error_handler 这个函数,可以用户自己捕捉这些异常,比如:

<?php
set_error_handler("exception_error_handler");
try{
strpos();
}catch(Exception $e){
echo $e->getMessage();
}
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
  这样就可以捕捉异常了
页: [1]
查看完整版本: PHP的异常捕捉