Handle Javascript errors with try-catch, throw and finally

PHOTO EMBED

Wed Jan 01 2020 19:00:00 GMT+0000 (Coordinated Universal Time)

Saved by @new_roman11 #javascript #errors #howto

try {
  let hello = prompt("Type hello");
  if (hello !== 'hello'){
    throw new Error("Oops, you didn't type hello");
  }
}
catch(e) {
  alert(e.message);
}
finally {
  alert('thanks for playing!');
}
content_copyCOPY

try — code to try that may produce errors catch — code that will handle any errors throw — a keyword you can use to throw your own custom errors finally — code that will run after a try/catch block regardless of the outcome

https://codeburst.io/learn-how-to-handle-javascript-errors-with-try-throw-catch-finally-83b4f9ef8c6f