Home > Backend Development > PHP Tutorial > PHP的错误捕捉

PHP的错误捕捉

WBOY
Release: 2016-06-13 12:31:18
Original
818 people have browsed it

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);
	}
Copy after login

?

?

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template