check

Common
JVM
JS
Native
1.0
fun check(value: Boolean)
(source)

Throws an IllegalStateException if the value is false.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
var someState: String? = null
fun getStateValue(): String {
    val state = checkNotNull(someState) { "State must be set beforehand" }
    check(state.isNotEmpty()) { "State must be non-empty" }
    // ...
    return state
}

// getStateValue() // will fail with IllegalStateException

someState = ""
// getStateValue() // will fail with IllegalStateException

someState = "non-empty-state"
println(getStateValue()) // non-empty-state
//sampleEnd
}
Common
JVM
JS
Native
1.0
inline fun check(value: Boolean, lazyMessage: () -> Any)
(source)

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

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
var someState: String? = null
fun getStateValue(): String {
    val state = checkNotNull(someState) { "State must be set beforehand" }
    check(state.isNotEmpty()) { "State must be non-empty" }
    // ...
    return state
}

// getStateValue() // will fail with IllegalStateException

someState = ""
// getStateValue() // will fail with IllegalStateException

someState = "non-empty-state"
println(getStateValue()) // non-empty-state
//sampleEnd
}