Error catch-bindings are finally optional (#tilPost)

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 sta…


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

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:


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2019-06-29T22:00:00+00:00) Error catch-bindings are finally optional (#tilPost). Retrieved from https://www.scien.cx/2019/06/29/error-catch-bindings-are-finally-optional-tilpost/

MLA
" » Error catch-bindings are finally optional (#tilPost)." Stefan Judis | Sciencx - Saturday June 29, 2019, https://www.scien.cx/2019/06/29/error-catch-bindings-are-finally-optional-tilpost/
HARVARD
Stefan Judis | Sciencx Saturday June 29, 2019 » Error catch-bindings are finally optional (#tilPost)., viewed ,<https://www.scien.cx/2019/06/29/error-catch-bindings-are-finally-optional-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » Error catch-bindings are finally optional (#tilPost). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2019/06/29/error-catch-bindings-are-finally-optional-tilpost/
CHICAGO
" » Error catch-bindings are finally optional (#tilPost)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2019/06/29/error-catch-bindings-are-finally-optional-tilpost/
IEEE
" » Error catch-bindings are finally optional (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2019/06/29/error-catch-bindings-are-finally-optional-tilpost/. [Accessed: ]
rf:citation
» Error catch-bindings are finally optional (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2019/06/29/error-catch-bindings-are-finally-optional-tilpost/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.