Javascript ES6 Concepts

ES6 Javascript Concepts
ES6 Javascript Concepts

Try catch error

when to use?

we use it when we have to do something with network request, or fetch data.

But we don’t want to our code to stop if any error comes —- then using try catch error is ideal. (if we don’t use try catch, our execution will stop when there’s any error)

try{}
catch(error){}

try{
  console.log(John)
}
catch(error){
  console.log(error) // prints the error caused
//console.log('error came while fetching') --- prints the exact line
}

Workflow: How it works?

  • first the code in try is executed
  • if there is no error, catch is ignored
  • if there is error, catch is executed