当前位置: 首页 > 图文教程 > 网络编程 > PHP > PHP应用技巧:如何将代码中的通知和警告删除

PHP
PHP实例教程:Output Control输出函数
memcached和mysql主从环境下PHP开发
基于LAMP架构设计的WEB框架
PHP代码:验证IPV6地址是否合法的正则
PHP环境快读搭建绿色软件包PHPnow
PHP教程:$_SERVER的详细参数整理
php获取url字符串截取路径的文件名和扩展名的函数
在命令行下运行PHP脚本[带参数]的方法
PHP 实用代码收集
PHP 时间转换Unix时间戳代码
关于php fread()使用技巧
PHPMailer 中文使用说明小结
php addslashes和mysql_real_escape_string
php cout<<的一点看法
PHP 变量的定义方法
php学习之 认清变量的作用范围
php 静态变量与自定义常量的使用方法
认识并使用PHP超级全局变量
通过具体程序来理解PHP里面的抽象类
php读取xml实例代码

PHP应用技巧:如何将代码中的通知和警告删除


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 62 ::
收藏到网摘: n/a

警告有时可以从一些代码中删除,当代码中弹出警告提示时,用户可进行适当选择,其中包括将它们写在错误日志中,或完全忽视。而Alexander Netkachev却有不同的解决方案——通过内建在PHP中的例外报告来处理、该编码技巧将展示如何通过try/catch语句以例外的方式来处理PHP通知和警告。

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

代码如下:

function errorHandler($errno, $errstr, $errfile, $errline) {

throw new Exception($errstr, $errno);

}

set_error_handler('errorHandler');

try {

file_put_contents('cosmos:\\1.txt', 'asdf');

} catch (Exception $e) {

echo $e->getMessage();

}

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:

class IOException extends Exception {}function errorHandler($errno, $errstr, $errfile