Error handling is the managing of errors
• Show error feedback
• local logging
• remote logging
Propagating errors refers to the process of passing an error from one part of the code to another using the throw statement with a try-catch statement.
try {
//do whatever
} catch|(error) {
// deal with the error
console.log(error.message)
} finally {
// do something everytime
}
Best practice for Error Handling
- Always use try catch around code that can fail
- Use descriptive error messages
- avoid swallowing errors
- log errors appropriately
Types of Errors
- Syntax error
- Reference error
- Type Error
- Range Error
Syntax Error
console.log("Hello world"
Reference Error
Using a variable with declaring the variable
console.log( neverDefinedVariable )
Type Error
const myNumber = 12
console.log( myNumber.toUpperCase() )
// number.toUpperCase is not a function
Range Error
var myArray = [1,2,3]
console.log(myArray[10]
// Index 10 is out of bounds
function userDataValidation() {
validateAge(17)
}
function validateAge(age) {
if (age < 17 ) {
throw new Error("Cant vote")
}
}