我已经使用try-catch多年了,但我从来没有学过如何以及何时使用finally,因为我从未明白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);
}
Run Code Online (Sandbox Code Playgroud)
终于正确使用了吗?
更准确的问题:
我可以使用finally(在未来的PHP版本或其他语言中)处理"创建一些不存在的东西"操作吗?