最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > php教程

如何将代码中的通知和警告删除

日期:06-24    来源:中国设计秀    作者:cnwebshow.com

    警告有时可以从一些代码中删除,当代码中弹出警告提示时,用户可进行适当选择,其中包括将它们写在错误日志中,或完全忽视。而Alexander Netkachev却有不同的解决方案——通过内建在PHP中的例外报告来处理、p3e中国设计秀

    该编码技巧将展示如何通过try/catch语句以例外的方式来处理PHP通知和警告。p3e中国设计秀

    尽管这是一个很简单的方案,但却完全可以帮助用户将所有的错误信息存储在同一位置。Alexander Netkachev所提供的代码示例既展示了基本的解决方案,也展示了其复杂的一面。另外,还为不同的例外类型提供了更详细的信息。p3e中国设计秀

   代码如下:p3e中国设计秀

   function errorHandler($errno, $errstr, $errfile, $errline) {p3e中国设计秀
throw new Exception($errstr, $errno);p3e中国设计秀
}p3e中国设计秀
set_error_handler('errorHandler');p3e中国设计秀
try {p3e中国设计秀
file_put_contents('cosmos:1.txt', 'asdf');p3e中国设计秀
} catch (Exception $e) {p3e中国设计秀
echo $e->getMessage();p3e中国设计秀
}p3e中国设计秀

The code above throws an exception because the file cannot be saved. Then the exception is caught by the try/catch statement. With a little bit of additional error processing you can create even more reliable code:p3e中国设计秀

class IOException extends Exception {}

function errorHandler($errno, $errstr, $errfile, $errline) {
	if (false !== substr('failed to open stream', $errstr)) {
		throw new IOException($errstr, $errno);
	}
	throw new Exception($errstr, $errno);
}

set_error_handler('errorHandler');

try {
	file_put_contents('cosmos:1.txt', 'asdf');
} catch (IOException $e) {
	echo 'IO exception: ' . $e->getMessage();
} catch (Exception $e) {
	echo 'Unknown exception: ' . $e->getMessage();
}
本文引用地址:/bc/article_58176.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明