DEV Community

Ovienadu Ken
Ovienadu Ken

Posted on

Try-Catch vs if-else

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?

😅

Oldest comments (5)

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.

Collapse
 
itsdarrylnorris profile image
Darryl Norris

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.

Collapse
 
alexluong profile image
Alex Luong • Edited

Since you're talking about try-catch in JavaScript specifically, how do you translate this into if-else?

async function hello() {
  try {
    await doSomething()
  } catch (e) {
    console.error(e)
  }
}

Or how do you translate this into try-catch:

if (arr.length === 0) doSomething()
else doSomethingElse()

I feel like try-catch and if-else are 2 different things and not interchangeable, unlike loop and recursion.

Collapse
 
ahferroin7 profile image
Austin S. Hemmelgarn

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:

try {
    function1()
    function2()
    function3()
} catch (error) {
    // This code will get run if any of the above functions throws an error
}
Enter fullscreen mode Exit fullscreen mode

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 the error 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.

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.