DEV Community

Discussion on: Try-Catch vs if-else

Collapse
 
thomasjunkos profile image
Thomas Junkツ • Edited

The main difference between if-else and try-catch language independent is, that you could miss a simple case and would not necessary notice, that you are something missing. If you miss to catch some kind of exception, your production code will tell you that immediately. It's easy to fail silently with if-else. If you are working with typed languages, you would not catch all, you would only catch a specific exception, which would be relatively narrow in scope. You typically do not catch (Exception e)(unless you do :D). Most of the time you catch something like InvalidArgumentException. When it make sense, you catch whole families of exceptions.

In javascript this is currently not a thing. You catch your exception and are mostly done. There are experiments with Conditional catch clauses.