Fred Friis
07/19/2023, 1:39 PMis VirtualMachineError, is ThreadDeath, is InterruptedException, is LinkageError -> false
instead of
is Error, is InterruptedException -> false
? and why isn't CancellationException included in the list of exceptions that is rethrown?
https://kotlinlang.slack.com/archives/C5UPMM0A0/p1592481003399300?thread_ts=1592477439.391400&cid=C5UPMM0A0Fred Friis
07/19/2023, 2:01 PMpublic actual fun NonFatal(t: Throwable): Boolean =
when (t) {
is ControlThrowable, is CancellationException -> false
else -> true
}
but why is it the only one listed here 🫠now it seems like any other throwable including InterruptedException, TimeoutCancellationException etc is not rethrown, is that really correct?Wesley Hartford
07/19/2023, 2:09 PMTimeoutCancellationException
extends CancellationException
Fred Friis
07/19/2023, 2:10 PMFred Friis
07/19/2023, 2:11 PMFred Friis
07/19/2023, 2:18 PMpublic inline fun <R> runCatching(block: () -> R): Result<R> {
return try {
Result.success(block())
} catch (t: Throwable) {
when (t) {
is Error -> { throw t } //never catch any Errors
else -> { //t is Exception
if (t is CancellationException) {
//never catch CancellationException
throw t
} else {
//what remains are Exceptions which are safe to catch
Result.failure(t)
}
}
}
}
}
Wesley Hartford
07/19/2023, 2:43 PMError
subclasses because anyone can make one. I've worked with a library (can't remember which) that had their own Error
subclass, and it certainly wasn't fatal. The reactor project seems to have made a similar decision, only considering VirtualMachineError
, ThreadDeath
, and LinkageError
to be fatal (see reactor.core.Exceptions#isJvmFatal
).Wesley Hartford
07/19/2023, 2:45 PMNonFatal()
function unless I had a good reason to do something else.Wesley Hartford
07/19/2023, 2:46 PMkotlinx.coroutines.runInterruptible
which handles InterruptedException
as a cancellation.Fred Friis
07/19/2023, 2:47 PMFred Friis
07/19/2023, 2:47 PMstreetsofboston
07/19/2023, 3:09 PMFred Friis
07/19/2023, 3:11 PMsimon.vergauwen
07/19/2023, 3:22 PMThey're going to change this though, but it's a big breakage so not entirely sure how they're going to deal with that. Ktor has a custom exception for this to not rely on KotlinX'sextendsTimeoutCancellationException
CancellationException
TimeoutCancellationException
but IIRC there are some edge cases where withTimeout
doesn't work for Ktor 🤕simon.vergauwen
07/19/2023, 3:23 PMCancellationException
is being rethown. https://github.com/arrow-kt/arrow/blob/main/arrow-libs/core/arrow-core/src/jvmMain/kotlin/arrow/core/NonFatal.ktFred Friis
07/19/2023, 3:24 PMFred Friis
07/19/2023, 3:24 PMsimon.vergauwen
07/19/2023, 3:25 PMarrow.core.raise.catch
or Either.catch
it shouldn't catch fatal exceptions.Fred Friis
07/19/2023, 3:27 PMpublic inline fun <R> saferRunCatching(block: () -> R): Result<R> {
return try {
Result.success(block())
} catch (t: Throwable) {
when (t) {
is Error -> { throw t } //never catch any Errors
else -> { //t is Exception
if (t is CancellationException) {
//never catch CancellationException
throw t
} else {
//what remains are Exceptions which are safe to catch
Result.failure(t)
}
}
}
}
}
simon.vergauwen
07/19/2023, 3:32 PMis VirtualMachineError, is ThreadDeath, is InterruptedException, is LinkageError, is CancellationException
I would not rethrow all Error
for reasons mentioned above. This is also what you an find in Project Reactor in addition of InterruptedException
and CancellationException
but if memory serves me right InterruptedException
& CancellationException
also get a different treatment in Project Reactor but they're not considered in the isJvmFatal predicate.
It's also the same as what Scala does in NonFatal.simon.vergauwen
07/19/2023, 3:33 PMexpect/actual
and have a slightly more complex implementation as we have in Arrow.Fred Friis
07/19/2023, 3:34 PM