-3

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?

Community
  • 1
  • 1
aWebDeveloper
  • 36,687
  • 39
  • 170
  • 242
  • 4
    Neither, a for loop is the best... – NickHalden Aug 03 '10 at 13:29
  • It's generally a good idea to avoid using try/catch as a substitute for proper logic. – FrustratedWithFormsDesigner Aug 03 '10 at 13:30
  • if / else is for testing a condition and executing different code branches based on the outcome during regular operation. try / catch is for error handling, that is, take care of things that might go wrong although they normally shouldn't. – tdammers Aug 03 '10 at 13:31
  • 6
    Maybe we should be a bit nicer in our judgments to first-timers, to not put them off? Ok, maybe not a good, or not a real question, but give someone the possibility to rephrase and help in that process. – Abel Aug 03 '10 at 13:33
  • Following up on my own suggestion, I rewrote @user406659's question. This is my view of his question and I believe it is (now) a very genuine and correct question, that's found in about every programming textbook and worthy of a good answer ;-) – Abel Aug 03 '10 at 13:41
  • 1
    @Abel: you have completely rewritten the question based on your guess about the OP's knowledge and intention. I suggest that if this question better represents the OP's intention that they create a new question with this text. I agree that, as phrased now, this is a valid question. As originally presented, the question was very vague, and it was answered multiple times with more detail than provided by the question. – Scott Saunders Aug 03 '10 at 13:56
  • 1
    Duplicate: http://stackoverflow.com/questions/651619/what-is-the-advantage-of-using-try-catch-versus-if-else . Also, this: http://stackoverflow.com/questions/328976/thorough-use-of-if-statements-or-try-catch-blocks – Piskvor left the building Aug 03 '10 at 14:38

4 Answers4

10

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

Community
  • 1
  • 1
Scott Saunders
  • 29,840
  • 14
  • 57
  • 64
  • Actually, that depends on your point-of-view and the interpretation of the "question". Many textbooks suggest if/else as an alternative to try/catch: i.e., prevent the error, don't catch exceptions unless you really need to and are capable of recovering state and let real exceptions fire. – Abel Aug 03 '10 at 13:35
2

They have different purposes. It depends on the context. There is no better.

I suggest you read about Exceptions and the if-else statement.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • They have different purposes indeed, but try/catch is often a warning for code-smell and should be rewritten as if/else instead to correct program flow and allow input to be filtered. Most exceptions should be thrown, not caught and signify that input or conditions are beyond what was expected. – Abel Aug 03 '10 at 13:44
  • @Abel: As I said, it depends on the context. Generally speaking you cannot say that one is *better* than the other. – Felix Kling Aug 03 '10 at 14:01
2

Rule of thumb: don't use try/catch for controlling program flow, just use it to catch errors.

zwip
  • 119
  • 1
0

I think you don't really understand what's if/else and try/catch.

KahWee Teng
  • 13,658
  • 3
  • 21
  • 21