Kotlin Runtime: Check, Require and Assert

July 23, 2019

Runtime checks

  • require: check arguments. Throws an IllegalArgumentException if the value is false.
  • check: check states. Throws an IllegalStateException if the value is false.
  • assert: other usage like check results. Throws an AssertionError if the value is false and runtime assertions have been enabled on the JVM using the -ea JVM option.


Check arguments, throws an IllegalArgumentException if false.

fun save(age: Int) {
    require(age >= 0) { "Age must be positive integer: $age" }

    // save the data


Check states, throws an IllegalStateException if false.

class Worker {
    private var isRunning = false

    fun run() {
        check(isRunning == false) { "Working is already running" }


Check results, throws an AssertionError if false.

val name = getName()
assert(name.isNullOrEmpty() == false) { "getName result is null or empty" }
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.