PHP try-catch 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12377712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP try-catch not working
提问by Andrius Naru?evi?ius
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';
}
However instead of just going to catch part and returning nothingit shows a warning Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null givenin 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.
然而,而不只是想捉部分并返回nothing它显示一个警告Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given在开头的行while。我从来没有想过在 php 中使用异常,但是在 C# 中经常使用它们,而且在 PHP 中它们的工作方式似乎不同,或者像往常一样,我遗漏了一些明显的东西。
回答by Philipp
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.
你不能用 try-catch 块处理警告/错误,因为它们不是异常。如果要处理警告/错误,则必须使用set_error_handler.
But it's better to fix this issue, because you could prevent it.
但是最好解决这个问题,因为你可以防止它。
回答by Gilad Tiram
Exception is only subclass of Throwable. To catch error you can try to do one of the following:
异常只是 Throwable 的子类。要捕获错误,您可以尝试执行以下操作之一:
try {
catch (\Exception $e) {
//do something when exception is thrown
}
catch (\Error $e) {
//do something when error is thrown
}
OR more inclusive solution
或更具包容性的解决方案
try {
catch (\Exception $e) {
//do something when exception is thrown
}
catch (\Throwable $e) {
//do something when Throwable is thrown
}
BTW : Java has similar behaviour.
顺便说一句:Java 有类似的行为。
回答by datasage
In PHP a warning is not an exception. Generally the best practice would be to use defensive coding to make sure the result is what you expect it to be.
在 PHP 中,警告也不例外。通常,最佳实践是使用防御性编码来确保结果符合您的预期。
回答by normmcgarry
Welp, unfortunately this is the issue about PHP. Try/catch statements will catch Exceptions, but what you're receiving is an old-school PHP error.
好吧,不幸的是,这是关于 PHP 的问题。Try/catch 语句将捕获异常,但您收到的是一个老式的 PHP 错误。
You'll have to catch an error like this with: http://php.net/manual/en/function.set-error-handler.php
您必须通过以下方式捕获这样的错误:http: //php.net/manual/en/function.set-error-handler.php
Either that or check to see if $matrix is a mysqli_result object prior to performing mysqli_fetch_array.
或者在执行 mysqli_fetch_array 之前检查 $matrix 是否是一个 mysqli_result 对象。
回答by adhominem
PHP is generating a warning, not an exception. Warnings can't be caught. They are more like compiler warnings in C#.
PHP 正在生成警告,而不是例外。无法捕获警告。它们更像是 C# 中的编译器警告。

