Error Handling in javascript

Debugging

You can debug the code using debugger keyword. Just add this keyword before the line where you want to debug the code. In Webstom and browser as well, it shows the state of script at the debugging point.

Error handling

You can easily handle errors using try .. catch .. finally in JavaScript. Your code should be placed in try block. If any exception is thrown, catch block can be used to catch the exception and take appropriate action. Catch block gets executed only when the exception is thrown. But Finally block is always executed.

let b = 0
try {
  let a = 20/b
} catch (err) { 
  console.log(err)
}finally{
  console.log("This will be executed regardless of error")
}

Web development and Automation testing

solutions delivered!!