I'm wondering if there is any pattern to follow to...
# coroutines
a
I'm wondering if there is any pattern to follow to model exceptions taking into account not to swallow the
CancellationException
? For example I have
class MyApplicationDomainException(val cause: Exception /* The cause could be CancellationException */ ) : Exception
Now when I handle that exception,
Copy code
myUseCase.fold(success = { }, failure = { exception -> if (exception.cause is CancellationException) throw exception.cause else handleException(exception) }
Now the drawback of this approach is that everytime I do the fold, I have to do that if statement. Another problem is that CancellationException could be wrapped several levels inside the
cause
so I have to unwrap it in order to rethrow it. I'm interested to know how other devs handle such cases. Maybe I need to try a different approach?