ES2019 Made Error Checking Easier in JavaScript

ES2019 Made Error Checking Easier in JavaScript

The ECMAScript2019 specification means it is now optional to use the error parameter inside the catch block of a try…catch statement.

Originally published by Dr. Derek Austin at https://medium.com

Before recent changes to JavaScript language, a try…catch statement always needed the caught error to be referenced inside the catch block.

In modern JavaScript, referencing the caught error is now optional.

Let’s look at a demo on processing JSON objects to see how it works.

// Let's take this JavaScript object and turn it into JSON
// Stringify should work -- it turns objects to JSON strings
// Parse should fail -- it turns JSON strings to objects
const myJavaScriptObject = {
  dog: "🐕",
  cat: "🐈",
  koala: "🐨",
  count: 3
};

// Before ES2019 -- Need to reference caught error try { console.log(JSON.parse(myJavaScriptObject)); } catch (error) { console.log(error + ""); } // result: SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data

// Works in ECMAScript2019 -- Leave out error try { console.log(JSON.parse(myJavaScriptObject)); } catch (error) { console.log("Whoops! That did not seem to be a JSON String... "); } // result: Whoops! That did not seem to be a JSON String...

// ES2019: No error, no problem -- we don't even really have to "catch it" as a parameter try { console.log(JSON.stringify(myJavaScriptObject)); } catch { console.log("Whoops! That did not seem to be a JSON String... "); } // result: {"dog":"🐕","cat":"🐈","koala":"🐨","count":3}

Not sure how JSON.parse() works?

Try…catch statements are commonly used with promises and asynchronous functions

The Takeaway

Is not referencing caught errors always useful? No, of course not. There are times when you do want to display the caught error itself.

However, there are cases where the error is not particularly informative, such as the SyntaxError that comes from JSON.parse().

In those cases, it can be useful to not reference the caught error by name.

Thanks for reading

If you liked this post, please do share/like it with all of your programming buddies!

Follow us on Facebook | Twitter

Further reading about JavaScript

The Complete JavaScript Course 2019: Build Real Projects!

Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)

JavaScript Bootcamp - Build Real World Applications

The Web Developer Bootcamp

JavaScript Programming Tutorial - Full JavaScript Course for Beginners

New ES2019 Features Every JavaScript Developer Should Know

Best JavaScript Frameworks, Libraries and Tools to Use in 2019

JavaScript Basics Before You Learn React

Build a CMS with Laravel and Vue

Google’s Go Essentials For Node.js / JavaScript Developers

7 best JavaScript Design Patterns You Should Know

12 Concepts That Will Level Up Your JavaScript Skills

React vs Angular vs Vue.js by Example

Microfrontends — Connecting JavaScript frameworks together (React, Angular, Vue etc)

Ember.js vs Vue.js - Which is JavaScript Framework Works Better for You



javascript web-development json

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Web Developer

Looking for an attractive & user-friendly web developer? HourlyDeveloper.io, a leading web, and mobile app development company, offers web developers for hire through flexible engagement models. You can **[Hire Web...

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.