Is this correct if I want to propagate errors but ...
# coroutines
e
Is this correct if I want to propagate errors but still know in my
finally
block that something failed?
Copy code
val token = acquireLock(state.entityId) ?: return finish()
var failed = false
try {
  doLockBasedOperation()
} catch (e: Exception) {
  failed = true
  throw e
} finally {
  withContext(NonCancellable) {
    releaseLock(state.entityId, token, failed)
  }
}
Will
finally
still run even though I rethrew in catch block? 🤔 Or am I supposed to duplicate the lock release into the
catch
block?
s
That looks fine to me 👍
thank you color 1
e
Nice, thanks!
d
Yeah, that's the way to do it.
Though I might invert that logic a little bit, and set a success flag inside the try block:
Copy code
val token = acquireLock(state.entityId) ?: return finish()
var success = false
try {
  doLockBasedOperation()
  success = true
} finally {
  withContext(NonCancellable) {
    releaseLock(state.entityId, token, !success)
  }
}
e
Hmm makes sense