Gli errori sincroni utilizzano try/catch/finally, e dovresti throw oggetti Error (non stringhe) in modo da ottenere uno stack trace.
js
{
data = .(input);
} (err) {
(err ) (err);
err;
} {
();
}
Gli errori sincroni utilizzano try/catch/finally, e dovresti throw oggetti Error (non stringhe) in modo da ottenere uno stack trace.
{
data = .(input);
} (err) {
(err ) (err);
err;
} {
();
}
// async/await → wrap awaits in try/catch
async function load() {
try {
return await fetch(url).then(r => r.json());
} catch (err) { showError(err); }
}
// promises → use .catch
fetch(url).then(use).catch(showError);
try {
setTimeout(() => { throw new Error("boom"); }, 0); // ❌ NOT caught
} catch (e) { /* never runs — the throw happens later, outside this stack */ }
Il callback setTimeout viene eseguito in un secondo momento, quindi il try circostante è ormai scomparso. Gestisci l'errore all'interno del callback.
window.addEventListener("unhandledrejection", e => log(e.reason)); // browser
process.on("unhandledRejection", err => log(err)); // Node
Error (o sottoclassi personalizzate) per stack trace e controlli instanceof.catch (e) {}). Registra sempre o rilancia.