Skip to content

Commit

Permalink
added
Browse files Browse the repository at this point in the history
  • Loading branch information
devrath committed Jan 16, 2024
1 parent 2f4406c commit 68e593e
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <------------->

}

0 comments on commit 68e593e

Please sign in to comment.