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

songyu277

songyu277

2016-01-29 13:23

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

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

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/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:

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/php/)
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();}

    原文地址:http://www.phpfreaks.com/articles/1964/0.php 

展开更多 50%)
分享

猜你喜欢

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

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

php中如何将图片储存在数据库里

编程语言 网络编程
php中如何将图片储存在数据库里

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

如何将win10系统中多余的字体删除掉?

windows10
如何将win10系统中多余的字体删除掉?

PHP中类的理解和应用[一]

PHP
PHP中类的理解和应用[一]

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

PHP中类的理解和应用[二]

PHP
PHP中类的理解和应用[二]

如何将代码生成的文件设为只读

ASP
如何将代码生成的文件设为只读

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

实例操作:PEAR的HTML_QuickForm7应用

实例操作:PEAR的HTML_QuickForm7应用

[初学VB.NET]如何防止重复打开MDI子窗体

[初学VB.NET]如何防止重复打开MDI子窗体
下拉加载更多内容 ↓