Hello I have a code block like so ```runBlocking {...
# coroutines
a
Hello I have a code block like so
Copy code
runBlocking {
  try {
    val response = withTimeout(timeout) {
      // okhttp network call 
    }
    // do something with Response
  } catch (e: TimeoutCancellationException) {
      // log exception
  } catch(e: Exception) {
    // log exception
  }
}
I am getting bunch of
Parent job is cancelling
exceptions in last catch block. This is not something that is locally reproducible but observed in data. what might be the possible reasons for this?
c
You are catching
CancellationException
, which you shouldn't do. Instead, you can write
Copy code
} catch (e: TimeoutCancellationException) {
      // log exception
  } catch(e: Exception) {
    coroutineContext.ensureActive()
    // log exception
  }
very nice 1
a
got it, but why should I get
Parent job is cancelling
message here?
c
Are you asking why the coroutine got cancelled? AFAIK, it's not caused by anything in the code you've shown.
a
My expectation is that I should always get
TimeoutCancellationException
or if its a
CancellationException
then
Job was cancelled
should be the message. Parent here is
runBlocking
, I am looking for possible reasons for which parent is getting cancelled before child. Could it be due to some unhandled exception in
// do something with Response
block?
c
It could be anything that cancels the parent, either by calling
cancel
or by throwing an exception in another coroutine that is a child of the same
Job
(here, `runBlocking`'s)
👍 1
thank you color 1