下面是我在函数中的 try catch
语句。我没有太多使用 try catch 语句,我想知道如何在 try catch 语句中返回值。我应该在 try 和 catch 语句之后返回一个值还是在 try block 中返回 OK?
function createBucket($bucket_name) {
if ($this->isValidBucketName($bucket_name)) {
if ($this->doesBucketExist($bucket_name)) {
return false;
}
else {
try {
$this->s3Client->createBucket(
array(
'Bucket' => $bucket_name,
'ACL' => CannedAcl::PUBLIC_READ
// Add more items if required here
));
return true;
}
catch (S3Exception $e) {
$this->airbrake->notifyOnException($e);
return false;
}
}
}
else {
$this->airbrake->notifyOnError('invalid bucket name');
return false;
}
}
最佳答案
is returning in the try block OK?
是的,是的。如果您需要返回那里的值,请执行此操作。
try {
function_that_throws_exception();
return true; // <-- This will never happen if an exception is raised
}
catch(Exception $e){
}
关于php - 尝试在 PHP 中捕获 - 我应该在 'try' block 中返回吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15052207/