🪟 Windows TippsModify Windows Support Phone Number with PowerShell(03.09.2026 um 00:00 Uhr)
🔧 AI Nachrichten Podcast: ChatGPT schwatzt Nutzern in Deutschland jetzt Werbung auf(28.08.2026 um 08:46 Uhr)
🪟 Windows TippsMicrosoft bringt Emoji 17.0 auf Windows 11(31.08.2026 um 08:16 Uhr)
🪟 Windows TippsModify Windows Support Phone Number with PowerShell(03.09.2026 um 00:00 Uhr)
🔧 AI Nachrichten Podcast: ChatGPT schwatzt Nutzern in Deutschland jetzt Werbung auf(28.08.2026 um 08:46 Uhr)
🪟 Windows TippsMicrosoft bringt Emoji 17.0 auf Windows 11(31.08.2026 um 08:16 Uhr)

🔧 Programmierung 🕛 vor 1 Jahr 3 Min Lesezeit
0

The difference between Error and Exception in JavaScript

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

https://github.com/Ray-D-Song




Error and exception are concepts born from practice, aiming to handle "programmable errors".






Error



From a code perspective, error tends to be manually handled precisely.


For example, fnA calls fnB and fnC. Both methods may encounter errors, and the processing code is roughly as follows:




CODE
function fnA() {
const { err: bErr, res: bRes } = fnB()
if (bErr) {
// ...
// error handling
}

const { err: cErr, res: cRes } = fnC()
if (cErr) {
// ...
// error handling
}
// normal logic
}






The key to "error" is to return an object or array from the function, with one field representing "an error occurred". As long as this field is not empty, programmers know that the normal flow has been interrupted.



JavaScript has an internal Error object and constructor, but the field representing the error is not required to be an Error object. Instead, the Error object is more often used in exception handling.






Exception



We already have error handling, why do we need exception?



Imagine a scenario where you have a button. When the button is clicked, it triggers function A. After going through multiple layers of calls (perhaps 10 layers), an error occurs in function X. You don't want to tell the user "unknown error", but rather want to provide specific information about what went wrong.



You can achieve this effect with errors, but you need to write ten times this code:




CODE
function fnA() {
const { err, res } = fnB()
if (err) {
// display error to user
showErr(err)
}
}

function fnB() {
const { err, res } = fnC()
if (err)
// propagate error
return { err, null }
}

// ... 10 similar passes

function fnY() {
const { err, res } = fnX()
if (err)
// propagate error
return { err, null }
}






This kind of boilerplate code is very inefficient. A better method is to use exceptions.



You only need to throw an exception when an error occurs in fnY. At the top level, you can catch it.




CODE
function fnA() {
try {
fnB()
} catch (e) {
showErr(e)
}
}

// ...

function fnY() {
const { err, res } = fnX()
if (err)
// 抛出
throw err
}






In this way, no matter where an error occurs, it can be caught at the top level, and the code in other layers is not affected.


Avoid polluting the entire code structure with errors at one place.





Why distinguish between the two?



We have already explained why we need exceptions, but why do we need to distinguish between errors and exceptions?



The best practice is to strictly distinguish between the two. If an error does not need to be passed up layer by layer, it should be handled directly in the current layer. For example, the error of fnC does not need to be used in fnA, so it should be handled as an error directly in B.



Assume that all errors are handled at the top level, then all logic is piled up in the catch block at the top level, which is difficult to maintain.




CODE
function main() {
try {
task1()
task2()
task3()
} catch(e) {
switch(e) {
case "type A":
//...
break;
case "type B":
//...
break;
case "type C":
//...
break;
}
}
}


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
1 Quelle
Modify Windows Support Phone Number with PowerShell
1 Quelle
Die Zukunft des Einkaufens: Warum wir ein neues Kapitel aufschlagen (und wie du es mitschreiben kannst)
1 Quelle
ZDE Podcast 251: Wie sieht digitales Instore Marketing 2026 aus, Amit Chatterjee?
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten The difference between Error and Exception in JavaScript

Thematisch verwandte Begriffe: difference, between, Error, Exception · 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 ...