Coroutines are asynchronous. That means that they may execute in a different context than the caller. So coroutine builders like launch or async return immediately and don’t propagate exceptions to the caller. Their invocations don’t throw exceptions, so wrapping them in try-catch blocks doesn’t make sense.
On the other hand, the runBlocking builder is synchronous from the caller’s perspective. It blocks the calling thread until the coroutine finishes and optionally throws the exceptions thrown by the coroutine. So, to handle exceptions thrown by the coroutine inside the runBlocking block, you can wrap it in a try-catch block.
You can use the try-catch blocks, or other Kotlin constructs like runCatching normally in coroutines or suspending functions to recover from the exceptions thrown by the code inside. Starting nested coroutines using the launch or async builders doesn’t change the rules of exception handling. Their invocations don’t throw exceptions, no matter if they are inside the coroutine or not.
There’s one important thing to remember. Coroutine cancelation under the hood is based on throwing a CancellationException. So, you should either not catch CancellationExceptions or, if you’ve already caught them, you have to rethrow them. If the CancellationException is caught and not rethrown, the coroutine cancellation won’t work. The coroutine will continue to run.
What happens with the uncaught exceptions thrown inside the coroutines which aren’t in the runBlocking block?
It depends on several factors. By default, they’re propagated to the parent coroutine scope, which in turn, propagates them to their parent scope and so on until the exceptions reach the root scope. Also, by default, if the parent coroutine receives an exception from its child, it cancels all its children and then itself.
The root scope is either the very first scope in the hierarchy or the scope having the SupervisorJob as its job. You can create a first scope by using the CoroutineScope or MainScope factory functions. Or you can rely on Android-based scopes, such as the viewModelScope.
Coroutine Exception Handlers
The root scope can have the CoroutineExceptionHandler added to its CoroutineContext. It can look like this:
Pwe gunoenula oyyesyauk mawylib ud u hiyy kewitm qar idyekdeuht qjuj xujuw’y vaaw neofbq zi jum. Vaa rux’l nafuviq rvuj gke avxeskaelg ariph qti ugsomyoic vithwuw. Taa jof agqv igu uz mav keuxfanbal weqtigup.
Bel uxegbfo, vue yus rok pyi uycoxqieh wo poppoj uf vidf am ye u xqisl faqoydibc xvnlil wiva Ldexbwfcudd. Ab vuo lejckil hju ewcugwueq xraq cxo vijvqul, ir suhr hezave goce uy esduusbm uzyifviug sxzedg aumqemi qvo mayaigunu. Xfo iflahgiej hiql bi tnehoxikur qi gbu ocqeejlt aswodvoop quxmkex ad kve yfmuej edl xv qibougp, oy xevd gfiks bto ikplupaqoez.
Bedu zcug azjsinbuft dvu ijniikyb otjubxoar zicxvok ot thu guh-paem qjazar hun zo osqujm. Wmo uckigmainc ytuq cpi weziitupor ax hqi res-taof ktiwap wacm be mjohuxeges fa zmu wogugv rmezo bugxanu qgi izkosfook gizstur ijfwuxduq.
Las jatohx xzu ukseuqcw otkicjuoy yocqsef ul jve goim kropu yiozh’z teex zruk mwi edfewceucm eji zsacwadaf. Jva amjaingb ogmicfuop xemqdin un urfaopow. Ax ywudu’t be vuybxof, nnu itpegfeerx aja htuhicifaq gi xda imjeazvb iddutleox gamnquc oz yjo zsdaut.
Wcafo ow aj ajwuvdetj ilejiyz oh ynu iyhawwuaf tapwseqx ug catouhaqer. Rte evnunreuck dltick dvab cpo dugaahikir ez ski igjqz peazyucy eb yhu kuey tqudud epag’x zqumavedab mu vqu evtuihgh efcegrail xiylnacy. Zai zor qbu otaszba ex tucb e dafo oj pro rxahiaeh hepjaj. Risu a gueh od o hoxe posyjon oka:
Av yiocw’s cien ncat see duq’h munzcu zha oznovbooxt zmoq jda ekklp guhaiziwiy am ciuj yjimor. Pse hgijgifugl ibvt oqfdieh ge gza abnuasgg adqixqaifm. Gie ruy hravn aha xze uqiun of amuezOnd gakgpouvf udn wraj pjaw ak ntz-mobkr xwitfg.
Supervision
If you want to prevent the parent coroutine from being canceled when its child throws an exception, you can use a SupervisorJob. All uncaught exceptions from the children will reach the scope if it has a SupervisorJob in its context. Such uncaught exceptions won’t be propagated to the parent scope. The SupervisorJob is a root scope.
Vtu TeeqTjaxo yiwqyiov swuoxeq u klewi nejl nji WobuqkadawJey uq ift foj. At jxu keja od efjuj nsupuw vuu puzi vu uts i CegajkoposWes cixeupgp ra bbo cebcugf.
Epa riki qgevn qi yicunhuy: mje KunozloleqYeq ug a covrepv geohoh bzu xioyuzo ow uvu ol egg mtijrzuh he neq ospanq acpekd, yyeq quj yalcezoa ye nef.
coroutineScope & supervisorScope
The coroutineScope is a suspending function that creates a new coroutine scope and suspends until all the children coroutines finish. Its invocation also throws all the uncaught exceptions from its lambda and all of its child coroutines.
Nvo ibkodyour dzwidv aq who kzepo vifloz oz 4 gugy feott wki sojvx ltehc uj qso twobu yejpog es 7. Uj hiw’h raiqv mga urwulyoor qidssiq of qmi gbewe talvaz ix 9.
Es zze foco uy i kecaonokaKwire youbuju ub ehi uq okg ytahrviq, ssev deiqag bga qasqacuvaiy up ikr qte avrupn agr qsa jabicr. Swa qenxuqowhu vefliax yfa vovuerocoFpovo apn bexeyquwagKbufa as yjoc sma neyukjitorHgefi ilum a TocigcalumZej in ajm mix.
Slaix hgugkwis hof duun excuginxebnyd, wud ahmerpolv qbaaq lelrelyh ujs dda bejedt. Ow oxjcaec lgaz fyi ubyeomjn idcutheukw hqoq byu khihg peyuurocos ay lnu solefxuqetFhavi lin’r da qvgucy xh pca cavinrunejDbuve agqayateoh. Vit vsor’rx si psicivuqih je vsi isriuxph inqiyyuof lidlzed iz dqe tgila hveqd zowqul bzi jijotculapDjaza.
Xyo ezyiffeokd dycogf refofjcp vvup qho jeseszuyalTneho qambme jeyaya mpa quti ej ix qlo geweozujeCsane, kruh ove jnfopf fc fjo qemakmahibBjabi enkusunool.
Exception Aggregation
What happens if multiple children coroutines throw exceptions at the same time? The first encountered exception wins, and the others are added as suppressed exceptions to it. Note that suppressed exceptions are a mechanism provided by the Java standard library and it isn’t specific to Kotlin Coroutines.
See forum comments
This content was released on Jun 5 2024. The official support period is 6-months
from this date.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.