File tree Expand file tree Collapse file tree 5 files changed +173
-4
lines changed
app/src/main/java/com/lukaslechner/coroutineusecasesonandroid/playground/flow/exceptionhandling Expand file tree Collapse file tree 5 files changed +173
-4
lines changed Original file line number Diff line number Diff line change @@ -3,20 +3,39 @@ package com.lukaslechner.coroutineusecasesonandroid.playground.flow.exceptionhan
33import kotlinx.coroutines.coroutineScope
44import kotlinx.coroutines.flow.Flow
55import kotlinx.coroutines.flow.flow
6+ import kotlinx.coroutines.flow.map
7+ import kotlinx.coroutines.flow.onCompletion
68import kotlinx.coroutines.launch
79
810suspend fun main (): Unit = coroutineScope {
911
1012 launch {
11- stocksFlow()
12- .collect { stock ->
13- println ( " Collected $stock " )
13+ val stocksFlow = stocksFlow()
14+ .map {
15+ throw Exception ( " Exception in Map " )
1416 }
17+
18+ try {
19+ stocksFlow
20+ .onCompletion { cause ->
21+ if (cause == null ) {
22+ println (" Flow completed successfully!" )
23+ } else {
24+ println (" Flow completed exceptionally with $cause " )
25+ }
26+ }
27+ .collect { stock ->
28+ println (" Collected $stock " )
29+ }
30+ } catch (e: Exception ) {
31+ println (" Handle Exception in catch block - $e " )
32+ }
1533 }
1634}
1735
1836private fun stocksFlow (): Flow <String > = flow {
1937 emit(" Apple" )
2038 emit(" Microsoft" )
21- throw Exception (" Network request failed" )
39+
40+ throw Exception (" Network Request Failed!" )
2241}
Original file line number Diff line number Diff line change 1+ package com.lukaslechner.coroutineusecasesonandroid.playground.flow.exceptionhandling
2+
3+ import kotlinx.coroutines.coroutineScope
4+ import kotlinx.coroutines.flow.Flow
5+ import kotlinx.coroutines.flow.catch
6+ import kotlinx.coroutines.flow.flow
7+ import kotlinx.coroutines.flow.onCompletion
8+ import kotlinx.coroutines.launch
9+
10+ suspend fun main (): Unit = coroutineScope {
11+
12+ launch {
13+ val stocksFlow = stocksFlow()
14+
15+ stocksFlow
16+ .onCompletion { cause ->
17+ if (cause == null ) {
18+ println (" Flow completed successfully!" )
19+ } else {
20+ println (" Flow completed exceptionally with $cause " )
21+ }
22+ }
23+ .catch { throwable ->
24+ println (" Handle exception in catch() operator $throwable " )
25+ }
26+ .collect { stock ->
27+ println (" Collected $stock " )
28+ }
29+ }
30+ }
31+
32+ private fun stocksFlow (): Flow <String > = flow {
33+ emit(" Apple" )
34+ emit(" Microsoft" )
35+
36+ throw Exception (" Network Request Failed!" )
37+ }
Original file line number Diff line number Diff line change 1+ package com.lukaslechner.coroutineusecasesonandroid.playground.flow.exceptionhandling
2+
3+ import kotlinx.coroutines.coroutineScope
4+ import kotlinx.coroutines.flow.Flow
5+ import kotlinx.coroutines.flow.catch
6+ import kotlinx.coroutines.flow.flow
7+ import kotlinx.coroutines.flow.onCompletion
8+ import kotlinx.coroutines.launch
9+
10+ suspend fun main (): Unit = coroutineScope {
11+
12+ launch {
13+ val stocksFlow = stocksFlow()
14+
15+ stocksFlow
16+ .onCompletion { cause ->
17+ if (cause == null ) {
18+ println (" Flow completed successfully!" )
19+ } else {
20+ println (" Flow completed exceptionally with $cause " )
21+ }
22+ }
23+ .catch { throwable ->
24+ println (" Handle exception in catch() operator $throwable " )
25+ emit(" Default Stock" )
26+ }
27+ .collect { stock ->
28+ println (" Collected $stock " )
29+ }
30+ }
31+ }
32+
33+ private fun stocksFlow (): Flow <String > = flow {
34+ emit(" Apple" )
35+ emit(" Microsoft" )
36+
37+ throw Exception (" Network Request Failed!" )
38+ }
Original file line number Diff line number Diff line change 1+ package com.lukaslechner.coroutineusecasesonandroid.playground.flow.exceptionhandling
2+
3+ import kotlinx.coroutines.coroutineScope
4+ import kotlinx.coroutines.flow.*
5+ import kotlinx.coroutines.launch
6+
7+ suspend fun main (): Unit = coroutineScope {
8+
9+ launch {
10+ val stocksFlow = stocksFlow()
11+
12+ stocksFlow
13+ .onCompletion { cause ->
14+ if (cause == null ) {
15+ println (" Flow completed successfully!" )
16+ } else {
17+ println (" Flow completed exceptionally with $cause " )
18+ }
19+ }
20+ .catch { throwable ->
21+ println (" Handle exception in catch() operator $throwable " )
22+ emitAll(fallbackFlow())
23+ }.catch { throwable ->
24+ println (" Handle exception in 2. catch() operator $throwable " )
25+ }
26+ .collect { stock ->
27+ println (" Collected $stock " )
28+ }
29+ }
30+ }
31+
32+ private fun stocksFlow (): Flow <String > = flow {
33+ emit(" Apple" )
34+ emit(" Microsoft" )
35+
36+ throw Exception (" Network Request Failed!" )
37+ }
38+
39+ private fun fallbackFlow (): Flow <String > = flow {
40+ emit(" Fallback Stock" )
41+
42+ throw Exception (" Exception in Fallback Flow" )
43+ }
Original file line number Diff line number Diff line change 1+ package com.lukaslechner.coroutineusecasesonandroid.playground.flow.exceptionhandling
2+
3+ import kotlinx.coroutines.coroutineScope
4+ import kotlinx.coroutines.flow.*
5+
6+ suspend fun main (): Unit = coroutineScope {
7+
8+ val stocksFlow = stocksFlow()
9+
10+ stocksFlow
11+ .onCompletion { cause ->
12+ if (cause == null ) {
13+ println (" Flow completed successfully!" )
14+ } else {
15+ println (" Flow completed exceptionally with $cause " )
16+ }
17+ }
18+ .onEach { stock ->
19+ throw Exception (" Exception in collect{}" )
20+ println (" Collected $stock " )
21+ }.catch { throwable ->
22+ println (" Handle exception in catch() operator $throwable " )
23+ }
24+ .launchIn(this )
25+ }
26+
27+ private fun stocksFlow (): Flow <String > = flow {
28+ emit(" Apple" )
29+ emit(" Microsoft" )
30+
31+ throw Exception (" Network Request Failed!" )
32+ }
You can’t perform that action at this time.
0 commit comments