Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable assertions in Kotlin?

I've created sample class to test assertions in Kotlin

class Assertion {
    fun sum(a: Int, b: Int): Int {
        assert(a > 0 && b > 0)
        return a + b
    }
}

fun main(args: Array<String>) {
    Assertion().sum(-1, 2)
}

and was using the following options but the program doesn't throw assert exception.

-ea:AssertionKt, -ea:Assertion and -ea:...

like image 704
Maxim Avatar asked Jun 19 '18 07:06

Maxim


People also ask

How do I enable assert?

To configure assertion options one must use either the -ea or -da command line flags to enable or disable assertions with the command line tool: “java”. For example, “java -ea Assert” where Assert is a java class file. You may also specify a specific class or package as follows.

What is assert in Kotlin?

assert(Boolean) throws AssertionError when its argument is false (but only if JVM assertions are enabled with -ea ). Use it to clarify outcomes and check your work.

How do you assert in Java?

An assertion is made using the assert keyword. Its syntax is: assert condition; Here, condition is a boolean expression that we assume to be true when the program executes.


1 Answers

To enable assertions in Kotlin, run the JVM with the -ea option, without any additional specifiers.

like image 75
yole Avatar answered Sep 18 '22 23:09

yole