DEV Community

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

Collapse
 
endy_tj profile image
Endy Tjahjono

I add a lot of validations using if. If I can't use if, for example using a function that may throw error, I will add try catch only if I want to do something when an exception is thrown. If there is nothing to do when an exception is thrown, I will let the exception bubble up. I have application level exception handling to log the error.

I avoid using try catch to control program flow because it is not easy to spot that a codeblock will emit an exception: you have to read the whole codeblock to find places that emit exception.