📚 › JavaScript: make pages do things
13. Errors and try/catch
Este artículo se traducirá automáticamente. Por ahora se muestra en inglés.
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.
Ver ejemplo
try {
null.length;
} catch (err) {
console.log("Something went wrong");
}
✍️ Ahora te toca
Use try and catch: in try throw new Error("oops"); in catch print the error message.