Kotlin checked exceptions alternative

Jocky Doe :

Since Kotlin doesn't support checked exceptions then how to make a programmer aware that a method may throw exception

Simple example:

class Calculator (value: Int = 0) {

    fun divide (dividend: BigDecimal, divider: BigDecimal) : BigDecimal {
        return dividend / divider
    }
}

Obviously the divide method may throw java.lang.ArithmeticException: Division by zero exception and the creator of the library needs to warn the user of the class to put the invoke in a try-catch clause

What's the mechanism for that awareness in Kotlin?

GhostCat salutes Monica C. :

Given the fact that the language doesn't have a construct to make this explicit, the only thing left is: implicitly.

For example by putting javadoc that clearly tells the user of the method about what/why exceptions might be thrown at him. Or you use the @Throws annotation.

Maybe, maybe the kotlin team will add compiler warnings at some point to make up for this ( see here ).

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=433388&siteId=1