尝试在 PHP 中 {inet_ntop($row['ip'])...} catch{graceively}
我正在研究从 MySQL 数据库打印地址的表。我不确定我是否很好地理解 try/catch 块。旧记录没有 IP 地址,但新记录在表中具有 IP 地址。新记录(带有 IP 地址)打印效果很好,但前提是我将其放入如下所示的 try catch 中:
try {
echo inet_ntop($row['ip']);
}
catch (Exception $e){
//echo 'Exception caught: ', $e->getMessage(), "\n";
echo "n/a";
}
IP 字段中没有 IP 的记录打印出一个丑陋的错误。如上所示,我注释掉了错误,但它还是打印了错误。如何正确打印包含当前 IP 地址(或缺少 0f)的表格,而不必查看所有这些错误:
Warning: inet_ntop() [function.inet-ntop]: Invalid in_addr value in/home/zp/public_html/example.COM/example.php on line 57
I am working on table that prints addresses from a MySQL database. I'm not sure I understand try/catch blocks very well. Old records have no IP address but new records do have an IP address in the table. The new records (with an IP address) print out fine, but only if I put it in a try catch like below:
try {
echo inet_ntop($row['ip']);
}
catch (Exception $e){
//echo 'Exception caught: ', $e->getMessage(), "\n";
echo "n/a";
}
The records that don't have an IP in the IP field print out an ugly error. As show above, I commented out the error, but it prints an error anyway. How can I properly print a table full of the present IP addresses (or lack 0f) without having to look at all of these errors:
Warning: inet_ntop() [function.inet-ntop]: Invalid in_addr value in/home/zp/public_html/example.COM/example.php on line 57
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
警告
无法捕获
。try..catch
块仅适用于异常。 警告是一种完全不同的错误报告机制。要抑制警告,可以使用错误控制运算符:当然,您应该首先验证传递给 inet_ntop 的值来完全避免警告。至少:
Warnings
are notcatch
able.try..catch
blocks work on Exceptions only. Warnings are a different error reporting mechanism entirely. To suppress warnings, you can use the error control operator:Of course, you should avoid warnings altogether by validating the values you pass into
inet_ntop
first. At the very least:inet_ntop
不会抛出您可以捕获的异常,例如:将按预期捕获,您应该做的是通过进行足够的检查来防止错误:
inet_ntop
does not throw an exception you can catch, for example:Will catch as expected, what you should be doing is preventing errors by doing sufficient checking:
您可以像这样抑制警告,尽管 RobertPitt 的答案要好得多:
You can suppress the warning like this, though RobertPitt's answer is far better: