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.')
}
More fire tips
Padding strings
Read fire tipRemoving duplication with ternary operators
The ternary operator can be used at many levels. The further we move it into a statement, the more duplication it can save us.
Read fire tip