Skip to content
techsnow

Async JavaScript

Error Handling

try...catch lets your program handle errors gracefully instead of crashing.

try {
  const data = JSON.parse("{ broken json");
} catch (error) {
  console.log("Something went wrong:", error.message);
} finally {
  console.log("This always runs");
}

Throwing your own errors

function divide(a, b) {
  if (b === 0) throw new Error("Cannot divide by zero");
  return a / b;
}