Skip to main content

no-throw-literal

Last updated: Jan 16, 2025

Disallow throwing literals as exceptions.

It is considered good practice to only throw the Error object itself or an object using the Error object as base objects for user-defined exceptions. The fundamental benefit of Error objects is that they automatically keep track of where they were built and originated.

Invalid:

throw "error";
throw 0;
throw undefined;
throw null;

Valid:

throw new Error("error");

Did you find what you needed?

Privacy policy