我想将一个throw
包装在一个helper函数中,用于日志(log)记录之类的目的.
private fun chooseEmailAddress(user: UserProfile): EmailAddress {
val emailAddress = user.emailAddresses.find {
true // some business logic
}
if (emailAddress == null) {
throwAndNotice(CustomError(
message = "No Email Address found.",
))
}
return emailAddress
}
private fun throwAndNotice(err: CustomError) {
NewRelic.noticeError(err)
throw err
}
问题:kotlin
个投诉类型不匹配:
Type mismatch.
Required: Email
Found: Email?
我猜编译器不知道throwAndNotice
总是抛出.如果我内联throwAndNotice
方法,它是有效的,但它会导致大约12个方法的重复.
有没有办法告诉编译器"下面的方法总是抛出的"?或者,有没有其他惯用的方式来处理这个问题?我不想求助于!!
美元.