Hello everyone. So I was thinking critically about the Try-Catch-finally blocks in Javascript and when logically comparing it to the if-else statements, I'm thinking the try-catch block is just a newer and fancier way to do if-else in certain situations.
Who wants to change my mind?
😅
Top comments (5)
First off, your statement is only accurate if you're dealing with manually generated errors/exceptions. Not all errors or exceptions are going to be something your code produces, some come from the JavaScript engine itself (or whatever libraries you are using.
Otherwise though, scoping is the big advantage of try-catch over if-else. More specifically, you can do stuff like this:
With if-else, you have to explicitly check the return value of each function.
A better analogy than if-else would be that it's a special syntax for a scoped handler for the
error
event. In effect, the contents of the catch block are equivalent to a single-argument function that runs as the handler for theerror
event, but only if the error comes from inside the try block. This is, in a way, similar to the cooncept of using POSIX singals to inform C (or C++, or whatever other language) programs about specific hardware or OS errors (see SIGFPE and SIGBUS for very specific examples).That said, I'd argue that JavaScript suffers from a questionable try-catch-finally design. The requirement to bind the exception to a variable and then inspect that in the catch handler to figure out what type of error even happened requires that you explicitly propagate any errors you don't handle, which in turn makes it easy to accidentally 'lose' errors that should indeed propagate.
The main difference between
if-else
andtry-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 withif-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 notcatch (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.
The problem with relying only on if/else without using try/catch is that it creates a lot of unnecessary code when you are trying to handle errors. With try/catch you can have multiple functions inside of a try and gracefully handle any errors in the catch.
A good example is Golang, does not have try/catch and most of the code is checking if a function it's returning an error or not.
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.
Since you're talking about try-catch in JavaScript specifically, how do you translate this into if-else?
Or how do you translate this into try-catch:
I feel like try-catch and if-else are 2 different things and not interchangeable, unlike loop and recursion.