Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我可以这样用try catch finally吗?_Php_Try Catch Finally - Fatal编程技术网

Php 我可以这样用try catch finally吗?

Php 我可以这样用try catch finally吗?,php,try-catch-finally,Php,Try Catch Finally,多年来,我一直在使用try catch,但我从未学会如何以及何时使用finally,因为我从未理解finally的意义(我读过坏书) 最后,我想问一下在我的案例中如何使用 我的代码示例应该解释一切: $s = ""; $c = MyClassForFileHandling::getInstance(); try { $s = $c->get_file_content($path); } catch FileNotFoundExeption { $c->creat

多年来,我一直在使用
try catch
,但我从未学会如何以及何时使用
finally
,因为我从未理解
finally
的意义(我读过坏书)

最后,我想问一下在我的案例中如何使用

我的代码示例应该解释一切:

$s = "";

$c = MyClassForFileHandling::getInstance();

try
{
    $s = $c->get_file_content($path);
}

catch FileNotFoundExeption
{
    $c->create_file($path, "text for new file");
}

finally
{
    $s = $c->get_file_content($path);
}
这是finally的正确用法吗

更确切的问题是:


我是否应该使用
finally
(在未来的PHP版本或其他语言中)来处理“如果不存在则创建内容”操作?

finally
直到尚未发布的5.5版才被引入PHP,因此您还没有看到任何与之相关的示例。因此,除非您正在运行PHP5.5的alpha版本,否则您还不能最终使用

来自手册()

在PHP5.5及更高版本中,还可以在catch块之后指定finally块。finally块中的代码总是在try和catch块之后执行,而不管是否抛出异常,也不管是否恢复正常执行

最后使用手册中的示例

<?php
function inverse($x) {
    if (!$x) {
        throw new Exception('Division by zero.');
    }
    else return 1/$x;
}

try {
    echo inverse(5) . "\n";
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
} finally {
    echo "First finally.\n";
}

try {
    echo inverse(0) . "\n";
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
} finally {
    echo "Second finally.\n";
}

// Continue execution
echo 'Hello World';
?>

Finally是指您最终想要做什么

try
{
    $s = $c->get_file_content($path);
}

catch FileNotFoundExeption
{
    $c->create_file($path, "text for new file");
}

finally
{
    //Create a pdf with my file
    //or, Rename my file
    //or, store my file into Database
}
无论try或catch内部发生什么(无论是否抛出异常),都将执行“Finally code”。 因此,在“try”和“finally”上使用相同的代码是没有意义的。
这仅仅回答了您的问题吗?

最终将始终执行,因此在本例中,这不是它的预期目的,因为正常执行会再次打开文件。如果你这样做,你打算做的事情也会以同样(更干净)的方式实现

$s = "";

$c = MyClassForFileHandling::getInstance();

try
{
    $s = $c->get_file_content($path);
}
catch(FileNotFoundExeption $e)
{
    $c->create_file($path, "text for new file");
    $s = $c->get_file_content($path);
}
然后手册上说:

对于以前没有遇到过finally块的人来说,它们与try/catch块后面的普通代码的主要区别在于,即使try/catch块将控制权返回给调用函数,它们也会被执行

如果:

  • 如果try块包含未捕获的异常类型,则执行此代码
  • 在catch块中抛出另一个异常
  • 您的try或catch阻塞调用返回
最后,在这种情况下会很有用:

function my_get_file_content($path)
{
    try
    {
        return $c->get_file_content($path);
    }
    catch(FileNotFoundExeption $e)
    {
        $c->create_file($path, "text for new file");
        return $c->get_file_content($path);
    }
    finally
    {
        $c->close_file_handler();
    }
}

=>如果您需要确保在这种情况下关闭文件处理程序,或者通常关闭某些资源。

我只想指定,如果
try
块中发生异常,即使
finally
块存在,也会正确引发异常。
finally
块的用途是提供干净、免费的资源。 我认为它的最佳用途是,例如,当您上载一个文件,但随后发生错误时:

$tmp_name = null;
try {
    $tmp_name = tempnam(UPLOAD_DIR, 'prefix');
    move_uploaded_file($file['tmp_name'], $tmp_name);
    ImageManager::resize($tmp_name, $real_path, $width, $height); // this will rise some exception
}
finally {
    if($tmp_name)
        unlink($tmp_name); // this will ensure the temp file is ALWAYS deleted
}
如您所见,通过这种方式,无论发生什么情况,临时文件都将被正确删除。
如果我们要在旧版本的PHP中模拟
finally
子句,我们应该编写如下内容:

// start finally
catch(Exception $ex) {
}
if($tmp_name)
    unlink($tmp_name);
if( isset($ex) )
    throw $ex;
// end finally

请注意,在
catch
块捕获某些内容的情况下,已重新引发异常。它的最终版本不清楚,但工作原理相同。

没错。但是OP中的例子是正确的(我想知道exception类是否应该用括号括起来)。也就是说,这里有一些关于如何使用“finally”的例子:来自wiki和Athos,这对你来说似乎很好:)Alpha 5今天发布:)你能帮我吗>错误>致命错误:在成功执行“catch”块后,不能在C:\wamp\www\anusthana\andro.php第55@john行的C:\wamp\www\anusthana\andro.php中使用try with catch或finally吗?@Ted:完整答案出来了。抱歉,如果我误导了:(@MayukhR,我需要更好的解释,但是+1可以帮助你。在你的情况下,最终可能会起作用,但我认为你使用它的方式不是一个好的做法。你应该在使用函数之前进行所有必要的检查,以回答你的问题:“我是否应该使用finally来处理”如果不存在的话创建“操作?”,我会说不。请参见下面您可能希望使用finally的时间。不,这是非常不正确的。finally子句用于清理,如关闭文件句柄、释放内存等。它实际上不是用于重试操作。如果最初的尝试成功,则会加载相同的数据两次,这是非常浪费的。really喜欢示例的感觉。谢谢:)@Kamil仔细看看:这里有一个try-and-catch块中的return语句。不过,最终还是会被执行。@mika,这就是区别!非常感谢。你终于为我揭开了这个谜:)还有一件事——什么时候执行?回来之前?回来后?或者涉及多个线程,这就是为什么PHP中没有
finally
?@Kamil欢迎您。调用return时,将执行finally,然后执行return。没有线程涉及。