尝试在 PHP 中 {inet_ntop($row['ip'])...} catch{graceively}

发布于 2024-11-18 22:58:51 字数 550 浏览 1 评论 0原文

我正在研究从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

玉环 2024-11-25 22:58:51

警告无法捕获try..catch 块仅适用于异常警告是一种完全不同的错误报告机制。要抑制警告,可以使用错误控制运算符

$ip = @inet_ntop($row['ip']);
echo $ip ? $ip : 'n/a';

:当然,您应该首先验证传递给 inet_ntop 的值来完全避免警告。至少:

echo $row['ip'] ? inet_ntop($row['ip']) : 'n/a;

Warnings are not catchable. 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:

$ip = @inet_ntop($row['ip']);
echo $ip ? $ip : 'n/a';

Of course, you should avoid warnings altogether by validating the values you pass into inet_ntop first. At the very least:

echo $row['ip'] ? inet_ntop($row['ip']) : 'n/a;
爱的那么颓废 2024-11-25 22:58:51

inet_ntop 不会抛出您可以捕获的异常,例如:

try
{
    test_function();
}
catch(Exception $e)
{
    //Relax
}

function test_function()
{
    throw new Exception("Something went wrong");
}

将按预期捕获,您应该做的是通过进行足够的检查来防止错误:

try
{
    $is_valid = filter_var($row['ip'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) || filter_var($row['ip'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
    if(!$is_valid)
    {
        throw new Exception('Invalid IP Address');
    }

    echo inet_ntop($row['ip']);
}
catch(Exception $e)
{
     echo 'n/A';
}

inet_ntop does not throw an exception you can catch, for example:

try
{
    test_function();
}
catch(Exception $e)
{
    //Relax
}

function test_function()
{
    throw new Exception("Something went wrong");
}

Will catch as expected, what you should be doing is preventing errors by doing sufficient checking:

try
{
    $is_valid = filter_var($row['ip'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) || filter_var($row['ip'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
    if(!$is_valid)
    {
        throw new Exception('Invalid IP Address');
    }

    echo inet_ntop($row['ip']);
}
catch(Exception $e)
{
     echo 'n/A';
}
方觉久 2024-11-25 22:58:51

您可以像这样抑制警告,尽管 RobertPitt 的答案要好得多:

echo(@inet_ntop($row['ip']));

You can suppress the warning like this, though RobertPitt's answer is far better:

echo(@inet_ntop($row['ip']));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文