catch无法正常工作

catch无法正常工作

本文介绍了PHP try-catch无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

try
{
    $matrix = Query::take("SELECT moo"); //this makes 0 sense

    while($row = mysqli_fetch_array($matrix, MYSQL_BOTH)) //and thus this line should be an error
    {

    }

    return 'something';
}
catch(Exception $e)
{
    return 'nothing';
}

但是,不仅要抓住一部分并返回nothing,它还会在以while开头的行中显示警告Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given.我从来没有想过在php中使用异常,但是在C#中经常使用它们,并且在PHP中似乎它们以不同的方式工作,或者像往常一样,我缺少明显的东西.

However instead of just going to catch part and returning nothing it shows a warning Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in the line starting with while. I have never came up to using exceptions in php, but used them a lot in C# and it seems in PHP they are working differently or, as always, I am missing something obvious.

推荐答案

您不能使用try-catch块处理警告/错误,因为它们不是例外.如果要处理警告/错误,则必须使用 set_error_handler .

You can't handle Warnings/Errors with try-catch blocks, because they aren't exceptions. If you want to handle warnings/errors, you have to register your own error handler with set_error_handler.

但是最好解决此问题,因为您可以防止它.

But it's better to fix this issue, because you could prevent it.

这篇关于PHP try-catch无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 03:39