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);
	}

     这样就可以捕捉异常了

猜你喜欢

转载自tankmarshal.iteye.com/blog/1989313