Regarding use of `NonFatal`. I can understand if y...
# arrow
t
Regarding use of
NonFatal
. 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?
Copy code
fun NonFatal(t: Throwable): Boolean =
  when (t) {
    is VirtualMachineError, is ThreadDeath, is InterruptedException, is LinkageError -> false
    else -> true
  }
s
Yes, they’re considered non-fatal and will be thrown.
It’s used in a different manner. If you use
Either.catch
these fatal errors won’t be caught, but will continue to propagate.
So we use this as a predicate
t
Ah I see. So I should not use this predicate for non-async error-handling purposes?
I remember seeing the same in Scala, but never really understood its purpose.
s
Yes, it’s very common practice in Scala. And it’s actually good practice to do. If you’re using Arrow, or Cat in Scala this is already done for you out-of-the-box
So you shouldn’t worry about it. If you’re doing a lot of custom
try/catch
with
e: Throwable
then it’s a good idea to use this predicate as well. We typically do the following.
Copy code
try { 
  donSomething()
} catch(e: Throwable) {
   ifError(e.nonFatalOrThrow())
}
t
I use something similar for Either:
Copy code
fun <Result> Either.Companion.fromTry(block: () -> Result): Either<Throwable, Result> =
    try {
        block().right()
    } catch (throwable: Throwable) {
        throwable.nonFatalOrThrow().left()
    }
..as we have not introduced use of coroutines yet.
👍 1
Does that seem ok? I am just worrying that some Errors might slip through by not being thrown, like custom ones introduced by libraries or similar.
s
That’s perfect, and it’ll catch all exceptions except for “is VirtualMachineError, is ThreadDeath, is InterruptedException, is LinkageError”. Also custom onces introduced by libraries.
t
So you are saying that the custom ones will be catched, not thrown?
🤔 1
s
Don’t you want all
NonFatal
to be captured in
Left<Throwable>
?
throwable.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)
.
t
Good question! 😄 I was wondering if what I really wanted was
Left<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.
t
Yes, I do not want to catch
Error
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
Copy code
is VirtualMachineError, is ThreadDeath, is InterruptedException, is LinkageError -> false
..and not like this
Copy code
is Error, is InterruptedException -> false
It must mean that there is some
Error
that is not considered fatal.
But i will not continue to bore you with such details. Thanks for the help and hope you have an error-free evening!
👍 1
197 Views