📖 INDEX
LOADING ENGINE...
JS catch block
Learn to master exceptions and keep your code fail-proof.
errorHandler.js
1 / 4
🛡️
Tutor:The 'catch' block is your safety net in JavaScript. It automatically executes if an error occurs within the 'try' block, preventing your application from crashing completely.
⭐ 0 EXP
What is the catch block?
In JavaScript, when code inside a try block fails, control is immediately passed to the catch block. This prevents the script from stopping abruptly and allows the programmer to handle the error in a controlled manner.
try {
// We try something risky
JSON.parse("INVALID_STRING");
} catch (error) {
// Here we handle the failure
console.log("Something went wrong: " + error.message);
}
Error Glossary
- SyntaxError
- Error in the code's grammar.
- ReferenceError
- When trying to access a variable that does not exist.