Errors happen! Maybe the user enters invalid data, a server is down, or a function receives the wrong input.
JavaScript provides try...catch blocks to handle errors gracefully instead of crashing the program.
try {
// Code that might throw an error
} catch (error) {
// Code to handle the error
}try {
let result = someUndefinedVariable + 10;
} catch (err) {
console.log("Something went wrong:", err.message);
}Output:
Something went wrong: someUndefinedVariable is not definedWithout try...catch, this error would stop the rest of your JavaScript from running!
finally always runs — whether there was an error or not.
try {
console.log("Trying something risky");
} catch (err) {
console.log("Error happened!");
} finally {
console.log("Always runs!");
}Use finally to clean up resources, close files/connections, or reset states.
You can throw custom errors manually using throw.
function divide(a, b) {
if (b === 0) {
throw new Error("Cannot divide by zero");
}
return a / b;
}
try {
console.log(divide(10, 0));
} catch (err) {
console.log("Caught:", err.message);
}| Error Type | Description |
|---|---|
ReferenceError | Variable not defined |
TypeError | Wrong type or invalid operation |
SyntaxError | Invalid JavaScript syntax |
RangeError | Number out of allowable range |
EvalError | Error in eval() function |
URIError | Invalid URI functions like decodeURI() |
function validateAge(age) {
if (isNaN(age)) throw new Error("Age must be a number");
if (age < 18) throw new Error("You must be at least 18");
return true;
}
try {
validateAge("hello");
} catch (err) {
console.log("Validation error:", err.message);
}You can catch an error, handle it partly, and re-throw it.
try {
try {
throw new Error("Original error");
} catch (err) {
console.log("Logging:", err.message);
throw err; // Re-throwing
}
} catch (e) {
console.log("Final catch:", e.message);
}async function fetchData() {
try {
let res = await fetch("https://invalid-url.com");
let data = await res.json();
} catch (error) {
console.log("Network or JSON error:", error.message);
}
}try...catch...finally to divide two numbers.try...catch block and re-throw the error.fetch() request error using try/catch.