php 返回,内部或外部尝试/捕获?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18963053/
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
Return, inside or outside Try / catch?
提问by JorgeeFG
In the below code, the IDE alerts me about "Missing return statement" in the last bracket. Which leads me to ask here if the return inside the try{}
is ok or should be outside it.
在下面的代码中,IDE 会在最后一个括号中提醒我“缺少返回语句”。这让我在这里问,里面的返回try{}
是否可以或应该在它外面。
Thanks a lot.
非常感谢。
public function getFileNamesFromKeywords( array $ids, $format ) {
try {
if(self::$dbLink) {
$ids = implode(',',$ids);
$query = 'SELECT d.id, d.wfid, d.docid , k.keyword, k.value'.
'FROM keywords k'.
'INNER JOIN documents d '.
'ON k.document_id = d.id'.
'WHERE k.document_id IN ('.$ids.')';
$results = self::$dbLink->query($query);
if( $results === false ) {
throw new Exception('Ocurrió un error al consultar a la DB.', 500);
}
$results = $results->fetchAll(PDO::FETCH_ASSOC);
$filenames = $this->buildFileNames( $results, $ids, $format );
}
else {
throw new Exception('No hay una conexión establecida con la DB.', 500);
}
return $filenames;
}
catch(Exception $e) {
$this->error = 'Error al intentar conectar con la BD: ' . $e->getMessage();
}
} //<----- Missing return statement
回答by Joni
If an exception is thrown and caught, what will the function return?
如果抛出并捕获异常,函数将返回什么?
You should have a return statement in the catch block, or after the try-catch block. Having a return statement in the try-block only is not enough.
您应该在 catch 块中或 try-catch 块之后有一个 return 语句。仅在 try 块中包含 return 语句是不够的。
回答by Brian Kinyua
if you place a return statement inside a function at any location then it's expected that the function has to return something and since you have placed the return statement inside a try-catch block ,when the IDE evaluates thw code it notices that you don't have a return statement for when your try fails that is in the catch.
如果您将 return 语句放在函数内的任何位置,那么预计该函数必须返回某些内容,并且由于您已将 return 语句放在 try-catch 块中,当 IDE 评估 thw 代码时,它会注意到您没有返回有一个返回语句,用于当您的 try 失败时在 catch 中。
I would recommended creating a $response variable initialized to false at the top of the function then assign the $filenames to it then after the try-catch block return the $response.
我建议在函数顶部创建一个初始化为 false 的 $response 变量,然后将 $filenames 分配给它,然后在 try-catch 块返回 $response 之后。
function getFilenames(){
$response = false;
try{
//your code
$response = $filenames;
}catch{
}
return $response;
}
By doing so you ensure that the function always returns something either the results you need or false.
通过这样做,您可以确保该函数始终返回您需要的结果或错误的结果。
回答by Gareth Luckett
The message you are being given is just a warning, as your code may not return anything. The best option to do as add a return to your catch if you want to stop the warning.
您收到的消息只是一个警告,因为您的代码可能不会返回任何内容。如果您想停止警告,最好的选择是为您的渔获物添加回报。
Just add the return before the closing brace.
只需在右大括号之前添加返回。
catch(Exception $e) {
$this->error = 'Error al intentar conectar con la BD: ' . $e->getMessage();
return null;
}