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

前端 未结 15 1548
挽巷
挽巷 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:10

    Generally you should do both. try/catch to avoid exceptional situations (file was suddenly deleted from another thread). And if/else to handle non-exceptional (check if file exists). Try/catch is relatively slower than a usual if/else so it does not worth to use it for everything.

提交回复
热议问题