Error handling with try-catch

With try and catch, we can react to errors in functions that could fail without our code crashing. If there is no error and the function succeeds, the catch-block is skipped.

// `try` to do something that could fail
try {
  // this may or may not be possible
  makeItBeFriday()

  // if we get here, the function worked
  console.log('It is Friday now!')
} catch (error) {
  // if something in `try` fails and throws an error, we end up here
  console.log('Sorry, it is not Friday now.')
}

There is more to learn

Get free previews of my upcoming course materials and other bonus content to help you work smarter. I share tips straight to your inbox once a week. You can read previous mails in the newsletter archive.

Cannot show form because cookie consent was not given.