🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)
🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)

🔧 Programmierung 🕛 kürzlich 3 Min Lesezeit
0

How to Handle Unhandled Exceptions & Unhandled Promise Rejections in JavaScript and React

↗ Quelle (dev.to)
🗣️ Stimme:




🚨 How to Handle Unhandled Exceptions & Unhandled Promise Rejections in JavaScript and React



One of the biggest differences between a demo app and a production app is error handling.




A good application doesn't just work when everything is fine—it fails gracefully when something goes wrong.










🧠 1️⃣ What is an Unhandled Exception?



An unhandled exception is an error that is thrown but never caught.



Example:




CODE
function divide(a, b) {
if (b === 0) {
throw new Error("Cannot divide by zero");
}
return a / b;
}

divide(10, 0); // ❌ Uncaught Error






Since there's no try...catch, the application may crash or stop executing that code path.









✅ Handle It with try...catch






CODE
try {
divide(10, 0);
} catch (error) {
console.error(error.message);
}






✔ Prevents the app from crashing unexpectedly.









🧠 2️⃣ What is an Unhandled Promise Rejection?



When a Promise rejects and no .catch() (or try...catch with await) handles it.




CODE
fetch("/api/users")
.then(res => res.json());

// ❌ No .catch()






Or:




CODE
async function getUsers() {
const res = await fetch("/api/users");
return res.json();
}

// ❌ No try...catch












✅ Handle Async Errors Properly






CODE
async function getUsers() {
try {
const res = await fetch("/api/users");

if (!res.ok) {
throw new Error("Failed to fetch users");
}

return await res.json();
} catch (error) {
console.error(error);
}
}












⚛️ 3️⃣ Handling Errors in React



React Error Boundaries catch:



✔ Rendering errors



✔ Lifecycle errors



✔ Constructor errors



Example:




CODE
<ErrorBoundary>
<Dashboard />
</ErrorBoundary>












🚨 Error Boundaries DO NOT Catch



❌ Event handler errors




CODE
<button onClick={() => {
throw new Error("Boom");
}} />






❌ Async errors




CODE
setTimeout(() => {
throw new Error("Boom");
}, 1000);






❌ Promise rejections



You must handle these yourself.









🌐 4️⃣ Global Error Handling (Browser)



You can listen for uncaught JavaScript errors:




CODE
window.addEventListener("error", (event) => {
console.error("Unhandled Exception:", event.error);
});






Useful for:




  • Logging

  • Monitoring

  • Crash reporting









🌐 5️⃣ Global Promise Rejection Handling



Handle promises that nobody caught:




CODE
window.addEventListener("unhandledrejection", (event) => {
console.error("Unhandled Promise:", event.reason);
});






This is commonly used to send errors to monitoring tools.









📊 6️⃣ Log Errors to Monitoring Services



In production, don't just use:




CODE
console.error(error);






Use tools like:




  • Sentry

  • Bugsnag

  • Datadog



These provide:



✔ Stack traces



✔ User sessions



✔ Browser info



✔ Release tracking









🚨 Common Mistakes



❌ Empty catch blocks




CODE
try {
// code
} catch {}






👉 Silently ignores errors.






❌ Swallowing errors




CODE
catch (error) {
// do nothing
}






Always log or handle them appropriately.






❌ Assuming fetch() throws for HTTP errors




CODE
const res = await fetch("/users");






A 404 or 500 does not throw. You should check:




CODE
if (!res.ok) {
throw new Error("Request failed");
}












💡 Senior-Level Insight



A good error-handling strategy has multiple layers:





  • Local handling: try...catch for recoverable operations.


  • Component handling: Error Boundaries for UI rendering errors.


  • Global handling: window.onerror and window.onunhandledrejection.


  • Monitoring: Send errors to a centralized service instead of relying on console.error.



The goal isn't to hide errors—it's to capture them, recover when possible, and give users a graceful experience.









🎯 Interview One-Liner




Handle synchronous exceptions using try...catch, asynchronous errors using .catch() or try...catch with async/await, use React Error Boundaries for rendering errors, and implement global error listeners and monitoring tools to capture uncaught exceptions and promise rejections in production.










JavaScript #ReactJS #Frontend #ErrorHandling #WebDevelopment #InterviewPrep #EngineeringMindset #SoftwareEngineering

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
GPT-6 Astra Release Today? OpenAI’s Next Major AI Model Is Almost Here
1 Quelle
Apple accuses OpenAI of destroying evidence as trade-secrets fight intensifies
1 Quelle
Major AI platforms go down in unprecedented simultaneous outage
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten How to Handle Unhandled Exceptions & Unhandled Promise Rejections in JavaScript and React

Thematisch verwandte Begriffe: Handle, Unhandled, Exceptions, Promise · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...