Interaction of AnkoLogger.error and kotlin.error


kotlin.error, in the standard library’s Preconditions.kt, throws an IllegalStateException. It’s very helpful to use. Meanwhile, AnkoLogger.error is a wrapper for Android’s Log.e. I’m finding that I really want to do both of these things at the same time.

My workaround is to have this:

import org.jetbrains.anko.AnkoLogger
import org.jetbrains.anko.error

 * Merge together the functionality of AnkoLogger.error and Kotlin's intrinsic error function,
 * which throws an IllegalStateException
fun AnkoLogger.throwError(message: Any, thr: Throwable? = null) {
    error(message, thr) // AnkoLogger
    kotlin.error(message) // throws IllegalStateException

The problem is that, even if you declare this function to be inline, Kotlin treats it differently than a direct call to kotlin.error, where the compiler will infer that the function is never returning, and won’t give you grief about “A return expression required in a function with a body block”. kotlin.error itself works around this by having an internal annotation (@kotlin.internal.InlineOnly) which we’re not allowed to use in our own code.

Any thoughts on this? What’s the right way to have a throwError directive that merges the error reporting and the throwing with the inference that control isn’t coming back to the caller?


If your throwError function never returns normally, just tell it to the compiler making its return type to be Nothing:

fun AnkoLogger.throwError(message: Any, thr: Throwable? = null): Nothing {


Ahh, I was focused on the inline business rather than the return type. I suppose you could try to work “Nothing type inference” into the Kotlin compiler at some time in the future, but this will certainly do the job for now.


On the contrary, Nothing return type is intentionally required to be specified explicitly, even for functions with expression body.

And when you omit return type of a function with a block body it is supposed to be Unit, see