← Back to Course

🐞 Errors, Debugging, and Defensive Coding

Bugs are part of programming. The goal is not to avoid every mistake immediately, but to find problems quickly and write code that fails more safely.

Common Debugging Tools

  • console.log() for checking current values
  • Breakpoints for pausing and inspecting execution
  • Error messages and stack traces for locating failures

Example: Guard Against Bad Input

function divide(total: number, divisor: number): number {
  if (divisor === 0) {
    throw new Error("Cannot divide by zero");
  }

  return total / divisor;
}

Example: Try and Catch

try {
  console.log(divide(10, 0));
} catch (error) {
  console.error("Operation failed", error);
}
← Back to Code Foundations