DEV Community

Cover image for The difference between Error and Exception in JavaScript
Ray-D-Song
Ray-D-Song

Posted on

The difference between Error and Exception in JavaScript

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:

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

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

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:

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 }
}
Enter fullscreen mode Exit fullscreen mode

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.

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

// ...

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

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.

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

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay