DEV Community

Discussion on: Try-Catch vs if-else

Collapse
 
marcoscosta profile image
Marcos Costa • Edited

I don't know if I'm wrong, but when using if-else, you're expecting a specific kind of error or condition that may come from within your code. While using try catch, you're trying to deal with thing that could possibly go wrong and you can't do much about it. Those things are like networking, IO, third party services integrations...

Think this way, When sending a request from your code to get some useful data, the networking may fail, due to (idk, laws of physics). Then you use try-catch. But lets say, if the request fail, and for some reason you want say to the user that it did not fail because of your app, but someone else code, use a if-else in the response code to check if it was a internal server error or another thing.