Skip to content

Commit 2a2a09b

Browse files
Add playground files for exceptionhandling
1 parent e83a6ca commit 2a2a09b

File tree

6 files changed

+147
-0
lines changed

6 files changed

+147
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.lukaslechner.coroutineusecasesonandroid.playground.exceptionhandling
2+
3+
import kotlinx.coroutines.CoroutineScope
4+
import kotlinx.coroutines.Job
5+
import kotlinx.coroutines.launch
6+
7+
fun main() {
8+
9+
val scope = CoroutineScope(Job())
10+
try {
11+
scope.launch {
12+
functionThatThrowsIt()
13+
}
14+
} catch (e: Exception) {
15+
println("Caught: $e")
16+
}
17+
18+
Thread.sleep(100)
19+
}
20+
21+
fun functionThatThrowsIt() {
22+
throw RuntimeException()
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.lukaslechner.coroutineusecasesonandroid.playground.exceptionhandling
2+
3+
import kotlinx.coroutines.CoroutineExceptionHandler
4+
import kotlinx.coroutines.CoroutineScope
5+
import kotlinx.coroutines.Job
6+
import kotlinx.coroutines.launch
7+
8+
fun main() {
9+
10+
val exceptionHandler = CoroutineExceptionHandler { context, exception ->
11+
println("Caught $exception in CoroutineExceptionHandler")
12+
}
13+
14+
val scope = CoroutineScope(Job())
15+
16+
scope.launch {
17+
launch(exceptionHandler) {
18+
functionThatThrowsIt()
19+
}
20+
}
21+
22+
Thread.sleep(100)
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.lukaslechner.coroutineusecasesonandroid.playground.exceptionhandling
2+
3+
import kotlinx.coroutines.*
4+
5+
fun main() {
6+
7+
val exceptionHandler = CoroutineExceptionHandler { coroutineContext, throwable ->
8+
println("Caught exception: $throwable")
9+
}
10+
11+
val scope = CoroutineScope(Job())
12+
13+
scope.launch(exceptionHandler) {
14+
launch {
15+
println("Starting coroutine 1")
16+
delay(100)
17+
throw RuntimeException()
18+
}
19+
launch {
20+
println("Starting coroutine 2")
21+
delay(3000)
22+
println("Coroutine 2 completed")
23+
}
24+
}
25+
26+
Thread.sleep(5000)
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.lukaslechner.coroutineusecasesonandroid.playground.exceptionhandling
2+
3+
import kotlinx.coroutines.*
4+
5+
fun main() {
6+
7+
val exceptionHandler = CoroutineExceptionHandler { context, exception ->
8+
println("Caught $exception in CoroutineExceptionHandler")
9+
}
10+
11+
val scope = CoroutineScope(Job() + exceptionHandler)
12+
13+
scope.async {
14+
val deferred = async {
15+
delay(200)
16+
throw RuntimeException()
17+
}
18+
}
19+
20+
Thread.sleep(1000)
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.lukaslechner.coroutineusecasesonandroid.playground.exceptionhandling
2+
3+
import kotlinx.coroutines.coroutineScope
4+
import kotlinx.coroutines.launch
5+
import kotlinx.coroutines.runBlocking
6+
7+
8+
fun main() = runBlocking<Unit>() {
9+
10+
try {
11+
doSomeThingSuspend()
12+
} catch (e: Exception) {
13+
println("Caught $e")
14+
}
15+
16+
}
17+
18+
private suspend fun doSomeThingSuspend() {
19+
coroutineScope {
20+
launch {
21+
throw RuntimeException()
22+
}
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.lukaslechner.coroutineusecasesonandroid.playground.exceptionhandling
2+
3+
import kotlinx.coroutines.*
4+
5+
fun main() {
6+
7+
val ceh = CoroutineExceptionHandler { coroutineContext, throwable ->
8+
println("Caught $throwable in CoroutineExceptionHandler")
9+
}
10+
11+
val scope = CoroutineScope(Job())
12+
13+
scope.launch(ceh) {
14+
try {
15+
supervisorScope {
16+
launch {
17+
println("CEH: ${coroutineContext[CoroutineExceptionHandler]}")
18+
throw RuntimeException()
19+
}
20+
}
21+
} catch (e: Exception) {
22+
println("Caught $e")
23+
}
24+
}
25+
26+
Thread.sleep(100)
27+
}

0 commit comments

Comments
 (0)