require

inline fun require(value: Boolean) (source)

Throws an IllegalArgumentException if the value is false.

import kotlin.test.*

fun main(args: Array) {
//sampleStart
fun getIndices(count: Int) {
    require(count >= 0) { "Count must be non-negative, was $count" }
    // ...
}

val exception = assertFailsWith<IllegalArgumentException> {
    getIndices(-1)
}
println(exception.message) // Count must be non-negative, was -1
//sampleEnd
}
inline fun require(value: Boolean, lazyMessage: () -> Any) (source)

Throws an IllegalArgumentException with the result of calling lazyMessage if the value is false.

import kotlin.test.*

fun main(args: Array) {
//sampleStart
fun getIndices(count: Int) {
    require(count >= 0) { "Count must be non-negative, was $count" }
    // ...
}

val exception = assertFailsWith<IllegalArgumentException> {
    getIndices(-1)
}
println(exception.message) // Count must be non-negative, was -1
//sampleEnd
}