20 lines
813 B
Plaintext
Raw Normal View History

2020-06-30 12:48:39 +02:00
It is a bad practice to <code>throw</code> something that's not derived at some level from <code>Error</code>. If you can't find an existing <code>Error</code> type that suitably conveys what you need to convey, then you should extend <code>Error</code> to create one.
Specifically, part of the point of throwing \<code>Error</code>s is to communicate about the conditions of the error, but literals have far less ability to communicate meaningfully than \<code>Error</code>s because they don't include stacktraces.
== Noncompliant Code Example
----
throw 404; // Noncompliant
throw "Invalid negative index."; // Noncompliant
----
== Compliant Solution
----
throw new Error("Status: " + 404);
throw new Error("Invalid negative index.");{code}
----
include::../see.adoc[]