kotlin.require

require

inline fun require(value: Boolean)

Throws an IllegalArgumentException if the value is false.

import kotlin.test.*

fun main(args: Array<String>) {
//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)

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

import kotlin.test.*

fun main(args: Array<String>) {
//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
}

© 2010–2017 JetBrains s.r.o.
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/require.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部