close

no-throw-literal

Configuration

rslint.config.ts
import { defineConfig, js } from '@rslint/core';

export default defineConfig([
  js.configs.recommended,
  {
    rules: {
      'no-throw-literal': 'error',
    },
  },
]);

Rule Details

This rule restricts what can be thrown as an exception. When ESLint was originally written, only literals were forbidden, but the rule has since been expanded to disallow any expression which cannot possibly be an Error object.

Examples of incorrect code for this rule:

throw "error";

throw 0;

throw undefined;

throw null;

const err = new Error();
throw "an " + err;

const err2 = new Error();
throw `${err2}`;

Examples of correct code for this rule:

throw new Error();

throw new Error("error");

const e = new Error("error");
throw e;

try {
  throw new Error("error");
} catch (e) {
  throw e;
}

Original Documentation