Skip to content

Files

Latest commit

 

History

History
35 lines (27 loc) · 645 Bytes

missing-throw.md

File metadata and controls

35 lines (27 loc) · 645 Bytes

Pattern: Missing throw keyword

Issue: -

Description

Creating a new Error object without the throw keyword doesn't actually throw the error. This is usually a mistake as the error will be created but not thrown, leading to silent failures.

Examples

Example of incorrect code:

function validate() {
  if (!valid) {
    new Error("Invalid input");
  }
}

const check = () => {
  new TypeError("Type mismatch");
};

Example of correct code:

function validate() {
  if (!valid) {
    throw new Error("Invalid input");
  }
}

const check = () => {
  throw new TypeError("Type mismatch");
};