Arrow
04/01/2021, 10:39 AMfun foo(exc: Exception) or fun<T : Throwable> foo(exc: KClass<T>)
2. I can't assign a default value for later fun<T : Throwable> foo(exc: KClass<T> = InvalidParamExc::class)Roukanken
04/01/2021, 11:04 AMArrow
04/01/2021, 11:07 AMAlexis Manin
04/01/2021, 11:10 AMRoukanken
04/01/2021, 11:13 AMException just typealias of Java's java.lang.Exception (in case of Kotlin/JVM ofc) ?
Shouldn't just throwing your own subclass of it then be easily interoperable with Java?Roukanken
04/01/2021, 11:13 AMArrow
04/01/2021, 11:19 AMfun<T : Exception> nonNull(exc: Optional<T>) {
if (exc.isPresent) throw exc.get()
else throw CustomExc()
}Roukanken
04/01/2021, 11:30 AMorElseThrow() method
something like this:
class CustomException : Exception()
class Test {
val smthGoesWrong = true
fun workOrThrow(exceptionSupplier: Supplier<out Exception>? = null) {
if (smthGoesWrong) {
throw exceptionSupplier?.get() ?: CustomException()
}
}
}
Test().workOrThrow { IllegalArgumentException("") }Roukanken
04/01/2021, 11:32 AMfun workOrThrow(exceptionSupplier: Supplier<out Exception> = Supplier { CustomException() })Roukanken
04/01/2021, 11:33 AMthrow exceptionSupplier.get())Arrow
04/01/2021, 11:36 AMMatteo Mirk
04/06/2021, 8:56 AMRoukanken
04/06/2021, 8:58 AM