None of the answers really explain the problem. The real issue here is the exception code
} catch (e: Exception) {
e.message?.let { Log.e(Constants.TAG, e.message!!) }
}
This entire expression evaluates to returning Boolean?
. Why? It returns null (not Unit) when e.message
is null. Otherwise it returns the Boolean from Log.e()
because Log.e() returns a Boolean. Kotlin treats any final expression that evaluates to something other than Unit as a return expression. That means the entire function has an implied Boolean? return value.
There are a number of ways to address this. Calling return
after the log expression is the easiest fix. This explicitly tells the compiler that nothing should be returned.
I frequently get caught out by this because the logging functions returns a Boolean value or when using the ?. operator on the last line of a conditional expression.