Back

Error Handling (try/catch)

Things go wrong in code: APIs fail, data is missing, users enter bad input. Error handling lets you catch those problems and respond gracefully instead of crashing. Key concepts: - `try`: run the risky code - `catch`: handle the error if something goes wrong - `finally`: always runs, whether success or failure

try {
  const response = await fetch('/api/data')
  if (!response.ok) {
    throw new Error("Request failed")
  }
  const data = await response.json()
  console.log(data)
} catch (error) {
  console.log("Error:", error.message)
} finally {
  console.log("This always runs")
}

Roadmap