kawmra
04/24/2019, 7:02 AMResult<T>
.
I have the code like following:
launch {
runCatching { doSomething() }
.onSuccess {
doSuccessProcess(it)
}
.onFailure {
doFailureProcess()
}
}
I wanna handle errors if doSomething()
fails.
But this code catches CancellationException
as well when launch
was canceled.
Are there any problems? Can I just ignore the CancellationException
?gildor
04/24/2019, 7:04 AMgildor
04/24/2019, 7:04 AMgildor
04/24/2019, 7:04 AMkawmra
04/24/2019, 7:14 AMkawmra
04/24/2019, 7:14 AMgildor
04/24/2019, 7:30 AMgildor
04/24/2019, 7:31 AMrunCatching {
try { doSomething() } catch (e: CancellationException) { someDefault() }
}
gildor
04/24/2019, 7:34 AMkawmra
04/24/2019, 9:42 AMdoSomething()
returns value and it may throw exceptions too.
And I can’t list up those exceptions in advance.gildor
04/24/2019, 9:45 AMtry {
doSomething()
} catch(e: CancellationException) {
//Do nothning
} catch(e: Exception) {
doFailureProcess()
}
kawmra
04/24/2019, 11:55 AMval value = try {
doSomething()
} catch (e: CancellationException) {
return
} catch (e: Exception) {
doFailureProcess()
return
}
doSuccessProcess(value)
Yes, try/catch style is possible, but I prefer Result<T> style because it is more readable than try/catch one.
My question was "CancellationException will be caught by runCatching", and you did let me notice that it is a surely exception because it was cancelled so I need handle the exception.
So the question was already solved.
I am always grateful for your help!gildor
04/24/2019, 12:43 PM