I have the following Kotlin code:
fun getAdminUser(): User { return getAllUsers().first { it.userType == ADMIN } as User } If getAllUsers() doesn't have an element that matches the specified predicate, it throws a NoSuchElementException. I'm happy with this exception but would like to override the exception message to provide more context when it fails. Is it possible to do in Kotlin w/o try-catch?
getAllUsers().firstOrNull { it.userType == ADMIN } ?: throw NoSuchElementException("my custom exception message")