Possible Duplicate:
What is the advantage of using try {} catch {} versus if {} else {}
I'd like to know whether, generally speaking, it's better to use a try/catch construct or to try to prevent this 开发者_如何学运维exception by (excessive?) use of if/else. Example: should I catch a FileNotFoundException, or should I test and try to find the file first.
What would be the best approach?
They are totally different. They are not interchangeable, and which is appropriate is determined by the requirement and context.
EDIT: This answer is based on the original, unedited question. The original, in it's entirety was:
PHP : if/else vs try/catch
which is better
They have different purposes. It depends on the context. There is no better.
I suggest you read about Exceptions
and the if
-else
statement.
Rule of thumb: don't use try/catch for controlling program flow, just use it to catch errors.
I think you don't really understand what's if/else and try/catch.
精彩评论