All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.javascript.rules.javascript.S3696.html Maven / Gradle / Ivy

There is a newer version: 10.17.0.28100
Show newest version

Why is this an issue?

In JavaScript, throwing literals (primitive values like strings, numbers, booleans, etc.) as exceptions is generally discouraged. While it is syntactically valid to throw literals, it is considered a best practice to throw instances of the Error class or its subclasses instead.

Throwing an instance of the Error class allows you to provide more meaningful information about the error.

The Error class and its subclasses provide properties like message and stack that can be used to convey useful details about the error, such as a description of the problem, the context in which it occurred, or a stack trace for debugging.

throw 404;                              // Noncompliant
throw "Invalid negative index.";        // Noncompliant

Throwing literals can make it harder to handle and differentiate between different types of errors. Instead, you should use one of the exception types specifically created for the purpose or define your own subclass of the Error class.

throw new Error("Status: " + 404);
throw new RangeError("Invalid negative index.");

Resources

Documentation





© 2015 - 2024 Weber Informatics LLC | Privacy Policy