DEV Community

Discussion on: The fight between Try-Catch and If-Else

Collapse
 
cereal84 profile image
Alessandro Pischedda

Mainly try-catch has use when the code (i.e. libraries) launch them because some "exceptional cases" so you're forced to catch and handle them.
In addition to this case the use of if-then or try-catch depends on your code and how do you handle things (errors, special cases, etc).
If it is a new project you can decide how to handle this, or if you're maintaining code of some one obviously you'll try to use it's way to code in order to keep the code style consistent.

If-else is, usually, used to handle logic into the code flow without wrap every 2 line of code.

Anyway it is up to you decide the style.