脚本宝典收集整理的这篇文章主要介绍了php – 我可以这样使用try-catch-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); }
问题是:
这是正确的用法吗?
编辑:更精确的问题:
我最后会使用(以后的PHP版本或其他语言)来处理“创建一些如果不存在”操作?
$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); }
然后手册说:
那么最后在这种情况下会是有用的:
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(); } }
以上是脚本宝典为你收集整理的php – 我可以这样使用try-catch-finally吗?全部内容,希望文章能够帮你解决php – 我可以这样使用try-catch-finally吗?所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。