Optional Catch Binding — ES2019 is coming…

Sérgio Vinícius de Sá Lucena
2 min readMay 28, 2019

--

As we all know, in the past years JavaScript has been evolving pretty fast and sometimes it's hard to keep track of many new features that are being released.

Since 2015, the Technical Committee 39 (TC39) decided to release a new version per year. This is when the term ES.Next became popular (or also ES6+). What happens is that they have a process (which I'll talk about in a future post) where they include in the next release all features that are finished by that year (or release date).

Today I will talk about one of the upcoming features that should be standardized in the ES2019 (or ES10) next month (June 2019). The list below contains all the new features that should be released next month and maybe you're lucky enough to start using it:

  • Optional Catch binding
  • JSON Superset
  • Object.fromEntries
  • Symbol.prototype.description
  • Function.prototype.toString revision
  • Well-formed JSON.stringify
  • String.prototype.{trimStart,trimEnd}
  • Array.prototype.{flat,flatMap}
  • String.prototype.matchAll

Today I chose to talk about the first one in the list:

Optional Catch Binding

If you ever had to add a try/catch in a situation that you didn't care about the error, maybe, like me, you also felt frustrated that you had to pass a parameter to the catch… Well… now you won't need it anymore.

This proposal will allow us to simply do:

While before you always had to do it with the parameter:

I tested the new feature in firefox 57.0.4 (an outdated version of the browser) and it failed as expected:

Optional catch binding failure example in an outdated version of Firefox

I ran it just in case you didn't remember it (or were too lazy to test it on the console 😜), as I wanted to show you how we always had to use it.

Ok, now you know you're not required to use it anymore, so does it mean you shouldn't? Not really! Actually, you should only use it if you want to completely ignore the error or if you don't really care about it as you probably know what it could be, and want to react to it somehow.

In general, it's a good practice to at least log the error in the console or, instead of assuming you know what the errors could be, check for unexpected type exceptions and rethrow them.

In any case, I think it's good to have the option, and we just have to use it safely and not make it a default behavior while coding.

--

--

Sérgio Vinícius de Sá Lucena
Sérgio Vinícius de Sá Lucena

No responses yet