返回,内部或外部尝试/捕获? [英] Return, inside or outside Try / catch?
本文介绍了返回,内部或外部尝试/捕获?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的代码中,IDE 会在最后一个括号中提醒我缺少返回语句".这让我在这里问 try{}
内的返回是否正常或应该在它之外.
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.
非常感谢.
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
推荐答案
如果抛出并捕获异常,函数会返回什么?
If an exception is thrown and caught, what will the function return?
您应该在 catch 块中或 try-catch 块之后有一个 return 语句.仅在 try 块中包含 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.
这篇关于返回,内部或外部尝试/捕获?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文