跳转到内容

useThrowOnlyError

此内容尚不支持你的语言。

Diagnostic Category: lint/style/useThrowOnlyError

Since: v1.8.0 Sources:

Disallow throwing non-Error values.

It is considered good practice only to 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.

throw undefined;
code-block.js:1:1 lint/style/useThrowOnlyError ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Throwing non-Error values is not allowed.

> 1 │ throw undefined;
^^^^^^^^^^^^^^^^
2 │

While Javascript supports throwing any value, handling non-Error values is confusing.

throw false;
code-block.js:1:1 lint/style/useThrowOnlyError ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Throwing non-Error values is not allowed.

> 1 │ throw false;
^^^^^^^^^^^^
2 │

While Javascript supports throwing any value, handling non-Error values is confusing.

throw "a" + "b";
code-block.js:1:1 lint/style/useThrowOnlyError ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Throwing non-Error values is not allowed.

> 1 │ throw “a” + “b”;
^^^^^^^^^^^^^^^^
2 │

While Javascript supports throwing any value, handling non-Error values is confusing.

throw new Error();
throw new TypeError('biome');
class CustomError extends Error {}
throw new CustomError();

This rule only covers cases where throwing the value can be known statically. Complex cases such as object and function access aren’t checked. This will be improved in the future once Biome supports type inference.