How to decide between using if/else vs try/catch?

前端 未结 15 1546
挽巷
挽巷 2020-12-05 06:44

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/c

15条回答
  •  无人及你
    2020-12-05 07:13

    As some answers have already pointed out, it depends.

    If/else are used for flow control, but so can Exceptions with the added plus of catching a error that occurs. But as Turowicz pointed out it's considered bad practice to a lot of people, to use try/catch more than the Minimum of what you have to.

    You can always read these articles from Ned Batchelder (Talks about return codes, as a alternative to using exceptions) and Joel Spolsky (Why he doesn't like programming with exceptions) to get a idea of what other think of exceptions and then make your own mind up.

提交回复
热议问题