42 lines
1.4 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
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.
2020-06-30 12:48:39 +02:00
[source,javascript,diff-id=1,diff-type=noncompliant]
2020-06-30 12:48:39 +02:00
----
if (x < 0) {
new Error("x must be nonnegative"); // Noncompliant: Creating an error without throwing it
2020-06-30 12:48:39 +02:00
}
----
You should make sure to always throw an error that you create using the `throw` keyword.
2020-06-30 12:48:39 +02:00
[source,javascript,diff-id=1,diff-type=compliant]
2020-06-30 12:48:39 +02:00
----
if (x < 0) {
throw new Error("x must be nonnegative");
}
----
== Resources
=== Documentation
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error[Error]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/throw[throw]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Control_flow_and_error_handling[Control flow and error handling]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Throw this error or remove this useless statement
include::../highlighting.adoc[]
endif::env-github,rspecator-view[]