Published at
Updated at
Reading time
2min
This post is part of my Today I learned series in which I share all my web development learnings.

I just made my way through my weekly newsletters and came across the optional catch-binding proposal which shipped in ES2019.

The language addition allows developers to finally omit the error argument when dealing with try/catch statements. This feature can become especially handy in modern JavaScript code that uses async/await a lot.

A few lines of code describe it best:

// classical try/catch
// โœ… works
try {
  throw new Error('oh noes');
} catch(e) {
  console.log(`Error!!! But, I don't care about the error object...`);
} 



// try/catch without error arguments
// โŒ throws "Uncaught SyntaxError: Unexpected token ')'"
try {
  throw new Error('oh noes');
  
//     ๐Ÿ‘‡ omitting the error argument doesn't work...
} catch() {
  console.log(`Error!!! But, I don't care about the error object...`);
} 



// try/catch without error binding
// โœ… works again ๐ŸŽ‰
// (across majors Edge, Firefox, Safari, Chrome)
try {
  throw new Error('oh noes');

//     ๐Ÿ‘‡ omitting `()` altogether works now ๐ŸŽ‰
} catch {
  console.log(`Error!!! But, I don't care about the error object...`);
}

This language addition was a little bit controversial, and people discussed it hotly in the proposal repository. Personally, I think it can be handy in some instances of quick prototyping where perfect error handling is secondary.

Reading about the feature, it turned out that major browsers (Chrome, Safari, Edge, Firefox) support the addition already. ๐ŸŽ‰ If you have to support more than the latest and greatest browsers, you can also use the babel plugin to use this new language feature today.

(The MDN docs are currently outdated, and a PR is pending)

If you want to learn more about it, here are some additional resources:

Was this TIL post helpful?
Yes? Cool! You might want to check out Web Weekly for more quick learnings. The last edition went out 6 days ago.
Stefan standing in the park in front of a green background

About Stefan Judis

Frontend nerd with over ten years of experience, freelance dev, "Today I Learned" blogger, conference speaker, and Open Source maintainer.

Related Topics

Related Articles