Trond Engell
06/18/2020, 10:50 AMNonFatal
. I can understand if you really want to handle InterrupedException
you might want to do that more explicitly, but what about errors that are not listed here (excluding subclasses)? Are they not fatal and to be thrown?
fun NonFatal(t: Throwable): Boolean =
when (t) {
is VirtualMachineError, is ThreadDeath, is InterruptedException, is LinkageError -> false
else -> true
}
simon.vergauwen
06/18/2020, 10:56 AMsimon.vergauwen
06/18/2020, 10:57 AMEither.catch
these fatal errors won’t be caught, but will continue to propagate.simon.vergauwen
06/18/2020, 10:58 AMTrond Engell
06/18/2020, 11:00 AMTrond Engell
06/18/2020, 11:01 AMsimon.vergauwen
06/18/2020, 11:02 AMsimon.vergauwen
06/18/2020, 11:03 AMtry/catch
with e: Throwable
then it’s a good idea to use this predicate as well.
We typically do the following.
try {
donSomething()
} catch(e: Throwable) {
ifError(e.nonFatalOrThrow())
}
Trond Engell
06/18/2020, 11:07 AMfun <Result> Either.Companion.fromTry(block: () -> Result): Either<Throwable, Result> =
try {
block().right()
} catch (throwable: Throwable) {
throwable.nonFatalOrThrow().left()
}
Trond Engell
06/18/2020, 11:09 AMTrond Engell
06/18/2020, 11:12 AMsimon.vergauwen
06/18/2020, 11:14 AMTrond Engell
06/18/2020, 11:21 AMsimon.vergauwen
06/18/2020, 11:24 AMNonFatal
to be captured in Left<Throwable>
?simon.vergauwen
06/18/2020, 11:26 AMthrowable.nonFatalOrThrow()
returns a NonFatal
exception, or it rethrows if the above predicate matches.
That means that all exceptions, except for VirtualMachineError
, ThreadDeath
, InterruptedException
& LinkageError
will be rethrown. All other exceptions will be returned as Left(exception)
.Trond Engell
06/18/2020, 11:31 AMLeft<Exception>
and to throw all Error
. In that case the predicate might not match all errors and I must do so manually. I am trying to wrap my head around this and I am unsure if this is good practice. I am sorry if this is not very Arrow-specific.simon.vergauwen
06/18/2020, 11:34 AMsimon.vergauwen
06/18/2020, 11:34 AMsimon.vergauwen
06/18/2020, 11:34 AMTrond Engell
06/18/2020, 11:50 AMError
in any way. Just throw them and crash/exit application to avoid bad state. I just figured there must be a reason why the predicate is defined like this
is VirtualMachineError, is ThreadDeath, is InterruptedException, is LinkageError -> false
..and not like this
is Error, is InterruptedException -> false
It must mean that there is some Error
that is not considered fatal.Trond Engell
06/18/2020, 11:50 AM