From 68e593ef43f0b2f0b444e7cd5f93500d9b6f20ff Mon Sep 17 00:00:00 2001 From: Devrath Date: Tue, 16 Jan 2024 21:06:24 +0530 Subject: [PATCH] added --- .../UsingLaunchExceptionHandleDemo.kt | 8 +- .../UsingLaunchExceptionHandleDemoVm.kt | 204 ++++++++---------- 2 files changed, 99 insertions(+), 113 deletions(-) diff --git a/application/KotlinConcepts/app/src/main/java/com/istudio/app/modules/module_demos/coroutines/exception_handeling/using_launch/UsingLaunchExceptionHandleDemo.kt b/application/KotlinConcepts/app/src/main/java/com/istudio/app/modules/module_demos/coroutines/exception_handeling/using_launch/UsingLaunchExceptionHandleDemo.kt index 61eab0f..864f9b4 100644 --- a/application/KotlinConcepts/app/src/main/java/com/istudio/app/modules/module_demos/coroutines/exception_handeling/using_launch/UsingLaunchExceptionHandleDemo.kt +++ b/application/KotlinConcepts/app/src/main/java/com/istudio/app/modules/module_demos/coroutines/exception_handeling/using_launch/UsingLaunchExceptionHandleDemo.kt @@ -26,14 +26,14 @@ fun UsingLaunchExceptionHandleDemo(navController: NavHostController) { Spacer(modifier = Modifier.height(20.dp)) - AppButton(text = "Demo") { - viewModel.demo() + AppButton(text = "No Exception Handler") { + viewModel.demo1() } Spacer(modifier = Modifier.height(20.dp)) - AppButton(text = "Cancel-Root") { - viewModel.rootCancel() + AppButton(text = "Catch locally using try catch") { + viewModel.demo2() } } diff --git a/application/KotlinConcepts/app/src/main/java/com/istudio/app/modules/module_demos/coroutines/exception_handeling/using_launch/UsingLaunchExceptionHandleDemoVm.kt b/application/KotlinConcepts/app/src/main/java/com/istudio/app/modules/module_demos/coroutines/exception_handeling/using_launch/UsingLaunchExceptionHandleDemoVm.kt index 0a71b22..e842860 100644 --- a/application/KotlinConcepts/app/src/main/java/com/istudio/app/modules/module_demos/coroutines/exception_handeling/using_launch/UsingLaunchExceptionHandleDemoVm.kt +++ b/application/KotlinConcepts/app/src/main/java/com/istudio/app/modules/module_demos/coroutines/exception_handeling/using_launch/UsingLaunchExceptionHandleDemoVm.kt @@ -15,131 +15,117 @@ class UsingLaunchExceptionHandleDemoVm @Inject constructor( ) : ViewModel() { private val ourScope = CoroutineScope(scopeJob + Dispatchers.Default) - fun demo() { - try { - ourScope.launch(CoroutineName("GrandParent")) { - try { - parent1Block() - parent2Block() - }catch (ex:Exception){ - println("Exception caught inside GrandParent scope") - } - }.invokeOnCompletion { - println("GrandParent invokeOnCompletion triggered") - } - }catch (ex:Exception){ - println("Exception caught outside GrandParent scope") - } - } + // <-----------------------> No Exception Handler <-----------------------> + fun demo1() { + ourScope.launch(CoroutineName("Parent")) { + try { + ourScope.launch(CoroutineName("Child-1")) { + try { + delay(10000) + }catch (ex : Exception){ + println("Child-1 throws exception(Normal catch) ${ex.localizedMessage}") + } + }.invokeOnCompletion { throwable -> + if(throwable!=null){ + // Exception thrown + println("Child-1 throws exception ${throwable.localizedMessage}") + }else{ + // Normal completion of coroutine + println("Child-1 is complete") + } + } - private fun parent1Block(){ - try { - ourScope.launch(CoroutineName("Parent-1")) { - try { - parent1Child1Block() - parent1Child2Block() - }catch (ex:Exception){ - println("Exception caught inside Parent-1 scope") + ourScope.launch(CoroutineName("Child-2")) { + /*try { + throw RuntimeException("Child-2 throws exception") + delay(10000) + }catch (ex : Exception){ + println("Child-2 throws exception(Normal catch) ${ex.localizedMessage}") + }*/ + throw RuntimeException("Child-2 throws exception") + }.invokeOnCompletion{ throwable -> + if(throwable!=null){ + // Exception thrown + println("Child-1 throws exception ${throwable.localizedMessage}") + }else{ + // Normal completion of coroutine + println("Child-2 is complete") + } } - }.invokeOnCompletion { - println("Parent-1 invokeOnCompletion triggered") + }catch (ex : Exception){ + println("Parent throws exception(Normal catch) ${ex.localizedMessage}") } - }catch (ex:Exception){ - println("Exception caught outside Parent-1 scope") - } - } - private fun parent2Block(){ - try { - ourScope.launch(CoroutineName("Parent-2")) { - try { - parent2Child1Block() - parent2Child2Block() - }catch (ex:Exception){ - println("Exception caught inside Parent-2 scope") - } - }.invokeOnCompletion { - println("Parent-2 invokeOnCompletion triggered") + + // Outer delay + delay(15000) + }.invokeOnCompletion{ throwable -> + if(throwable!=null){ + // Exception thrown + println("Parent throws exception ${throwable.localizedMessage}") + }else{ + // Normal completion of coroutine + println("Parent is complete") } - }catch (ex:Exception){ - println("Exception caught outside Parent-2 scope") } + } - private fun parent1Child1Block(){ - try { - ourScope.launch(CoroutineName("Parent-1-child-1")) { - try { - repeat(10){ - println("Parent-1-child-1 ------------------>$it") - delay(1000) + // <-----------------------> No Exception Handler <-----------------------> + + // <-----------------------> Catch locally using try catch <-------------> + fun demo2() { + + ourScope.launch(CoroutineName("Parent")) { + try { + ourScope.launch(CoroutineName("Child-1")) { + try { + delay(10000) + }catch (ex : Exception){ + println("Child-1 throws exception(Normal catch) ${ex.localizedMessage}") } - }catch (ex:Exception){ - println("Exception caught inside Parent-1-child-1 scope") - } - }.invokeOnCompletion { - println("Parent-1-child-1 invokeOnCompletion triggered") - } - }catch (ex:Exception){ - println("Exception caught outside Parent-1-child-1 scope") - } - } - private fun parent1Child2Block(){ - try { - ourScope.launch(CoroutineName("Parent-1-child-2")) { - try { - repeat(10){ - println("Parent-1-child-2 ------------------>$it") - delay(1000) + }.invokeOnCompletion { throwable -> + if(throwable!=null){ + // Exception thrown + println("Child-1 throws exception ${throwable.localizedMessage}") + }else{ + // Normal completion of coroutine + println("Child-1 is complete") } - }catch (ex:Exception){ - println("Exception caught inside Parent-1-child-2 scope") } - }.invokeOnCompletion { - println("Parent-1-child-2 invokeOnCompletion triggered") - } - }catch (ex:Exception){ - println("Exception caught outside Parent-1-child-2 scope") - } - } - private fun parent2Child1Block(){ - try { - ourScope.launch(CoroutineName("Parent-2-child-1")) { - try { - repeat(10){ - println("Parent-2-child-1 ------------------>$it") - delay(1000) + + ourScope.launch(CoroutineName("Child-2")) { + try { + throw RuntimeException("Child-2 throws exception") + delay(10000) + }catch (ex : Exception){ + println("Child-2 throws exception(Normal catch) ${ex.localizedMessage}") } - }catch (ex:Exception){ - println("Exception caught inside Parent-2-child-1 scope") - } - }.invokeOnCompletion { - println("Parent-2-child-1 invokeOnCompletion triggered") - } - }catch (ex:Exception){ - println("Exception caught outside Parent-2-child-1 scope") - } - } - private fun parent2Child2Block(){ - try { - ourScope.launch(CoroutineName("Parent-2-child-2")) { - try { - repeat(10){ - println("Parent-2-child-2 ------------------>$it") - delay(1000) + }.invokeOnCompletion{ throwable -> + if(throwable!=null){ + // Exception thrown + println("Child-1 throws exception ${throwable.localizedMessage}") + }else{ + // Normal completion of coroutine + println("Child-2 is complete") } - }catch (ex:Exception){ - println("Exception caught inside Parent-2-child-2 scope") } - }.invokeOnCompletion { - println("Parent-2-child-2 invokeOnCompletion triggered") + }catch (ex : Exception){ + println("Parent throws exception(Normal catch) ${ex.localizedMessage}") + } + + // Outer delay + delay(15000) + }.invokeOnCompletion{ throwable -> + if(throwable!=null){ + // Exception thrown + println("Parent throws exception ${throwable.localizedMessage}") + }else{ + // Normal completion of coroutine + println("Parent is complete") } - }catch (ex:Exception){ - println("Exception caught outside Parent-2-child-2 scope") } - } - fun rootCancel() { - scopeJob?.cancel() } + // <-----------------------> Catch locally using try catch <-------------> } \ No newline at end of file