Starlit Daily

📚 › JavaScript: make pages do things

13. Errors and try/catch

Code can fail. try runs code that might fail, and catch handles the error instead of stopping the whole program. Read the error message: it tells you what went wrong.

Handle errors where you can recover from them, such as a missing network response.

Show example

try {
  null.length;
} catch (err) {
  console.log("Something went wrong");
}

✍️ Your turn

Use try and catch: in try throw new Error("oops"); in catch print the error message.

← Back to course