In Kotlin, the error()
function is a part of the standard library, and it is specifically designed to throw an exception. When you call error(message: String)
, it internally throws an instance of IllegalStateException
with the given message.
Therefore, you do not need to use throw with error()
, as it already handles throwing the exception. Using throw in front of error()
would be redundant and may lead to confusion.
Using (snippet of Noncompliant Code):
throw error("fatal: this is an error")
is unnecessary and incorrect because error()
itself throws an exception.
Here’s the correct way to use it (snippet of Compilant Code):
error("fatal: this is an error")
This will immediately throw an IllegalStateException
with the message "fatal: this is an error"
.