Skip to content

Exception

Medium — good to knowGeneral Dev

ELI5 — The Vibe Check

An exception is a special kind of error that 'throws' itself up through your code like a hot potato, looking for someone to catch it. If nobody catches it, the whole program crashes. You catch exceptions with try/catch blocks and handle them gracefully instead of letting the program explode.

Real Talk

An exception is an event that disrupts normal program flow and is represented as an object containing error information. When code 'throws' an exception, it unwinds the call stack until a matching catch block is found or the program terminates. Exceptions can be built-in (TypeError, ValueError) or custom classes extending the base Error class.

Show Me The Code

// Throwing and catching an exception:
function divide(a, b) {
  if (b === 0) throw new Error("Cannot divide by zero");
  return a / b;
}

try {
  const result = divide(10, 0);
} catch (error) {
  console.error("Caught:", error.message);
} finally {
  console.log("This always runs");
}

When You'll Hear This

"Throw an exception if the input is invalid." / "The unhandled exception crashed the server."

Made with passive-aggressive love by manoga.digital. Powered by Claude.