Errors should not be created without being thrown because they can confuse and make it difficult to debug code. When an error is thrown, it means
that something unexpected has happened, and the program cannot continue executing as expected. By creating an error without throwing it, it may appear
as if everything is working correctly, but in reality, an underlying issue must be addressed.
if (x < 0) {
new Error("x must be nonnegative"); // Noncompliant: Creating an error without throwing it
}
You should make sure to always throw an error that you create using the throw
keyword.
if (x < 0) {
throw new Error("x must be nonnegative");
}