To handle errors in JavaScript, we wrap our code in a try...catch(err) block as follows:

try {
 // do something here
 // you can also throw an error with a custom Message using the Error() constructor for user-user-defined exceptions
 // runtime errors thrown will be "bubbled" to the catch(e) {} block below
} catch(err) {
  // handle all the errors here

Example fetching todo items from the dummyjson API

import axios from 'axios'

(async () => {
    try {
    const result = await axios.get('')
    const data = 
  } catch(err) {
    console.log(`Failed to Fetch todo Items with E:`, err)

That’s all. I’m working on the book titled: “A Modern JavaScript Primer”, please share your views here: 👉 What concepts do you find difficult to grasp in JavaScript?

Found this article helpful? You may follow me on Twitter where I tweet about interesting topics on software development.