[英]When you throw an exception in a coroutine scope, is the coroutine scope reusable?
我一直在用協程找出錯誤處理方面的問題,我已經按照以下步驟縮小了單元測試的范圍:
一切都很好。 但是,當我嘗試使用相同的協程范圍來啟動新的協程時,總是會例外地完成,但有相同的例外。
這是測試:
fun `when you throw an exception in a coroutine scope, is the coroutine scope dead?`() { val parentJob = Job() val coroutineScope = CoroutineScope(parentJob + Dispatchers.Default) val deferredResult = coroutineScope.async { throw IllegalStateException() } runBlocking { try { deferredResult.await() } catch (e: IllegalStateException) { println("We caught the exception. Good.") } try { coroutineScope.async { println("we can still use the scope") }.await() } catch (e: IllegalStateException) { println("Why is this same exception still being thrown?") } } }
這是測試的輸出:
We caught the exception. Good.
Why is this same exception still being thrown?
為什么會這樣呢?
我應該如何處理例外情況?
Either<Result, Exception>
嗎? 注意我正在使用Kotlin 1.3
當您在范圍內啟動協程(使用async
或launch
)時,默認情況下協程失敗會取消此范圍以立即取消所有其他子級。 這種設計避免了晃動和丟失異常。
這里的一般建議是:
除非確實需要並發,否則不要使用async
/ await
。 當使用暫停函數設計代碼時,不需要使用async
和await
。
如果確實需要並發執行,請遵循以下模式:
coroutineScope { val d1 = async { doOne() } val d2 = async { doTwo() } ... // retrieve and process results process(d1.await(), d2.await(), .... ) }
如果您需要處理並發操作的失敗,則在coroutineScope { ... }
周圍try { ... } catch { ... }
,以捕獲任何並發執行的操作中的失敗。
SupervisorJob
),它們允許進行細粒度的異常處理。 您可以在文檔https://kotlinlang.org/docs/reference/coroutines/exception-handling.html中閱讀更多內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.