如何使用的if / else VS try / catch语句之间做出选择? [英] How to decide between using if/else vs try/catch?
问题描述
在编写代码时,一个人如何使用if / else或try / catch语句之间做出选择?例如,在检查的文件,这应该基于的if / else(如果(File.Exists))或try / catch块?
When writing code, how does one decide between using if/else or try/catch? For example, in checking for a file, should this be based on if/else (if (File.Exists)) or a try/catch block?
例如,写入文件,可以通过处理的if / else块来创建一个文件,然后写它,或者假设一个try / catch说该文件存在。在选择有什么注意事项呢?
For example, writing to a file can be handled via an if/else block to create a file and then write to it, or a try/catch with an assumption that the file exists. What considerations are there in choosing?
感谢
推荐答案
您应该从不使用拉升流量控制/ catch语句。
You should never use try/catch for flow control.
生成异常是一个非常昂贵的行动。如果/别人更快,更清洁。
Generating an exception is an extremely expensive action. If/else is much faster and cleaner.
这篇关于如何使用的if / else VS try / catch语句之间做出选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!