Skip to content

Commit

Permalink
Make supervisor samples runnable (Kotlin#2897)
Browse files Browse the repository at this point in the history
  • Loading branch information
qwwdfsad committed Aug 26, 2021
1 parent dfa1b63 commit 3dcc349
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/topics/exception-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ only downwards. This can easily be demonstrated using the following example:
import kotlinx.coroutines.*

fun main() = runBlocking {
//sampleStart
val supervisor = SupervisorJob()
with(CoroutineScope(coroutineContext + supervisor)) {
// launch the first child -- its exception is ignored for this example (don't do this in practice!)
Expand All @@ -389,8 +390,10 @@ fun main() = runBlocking {
supervisor.cancel()
secondChild.join()
}
//sampleEnd
}
```
{kotlin-runnable="true" kotlin-min-compiler-version="1.3"}

> You can get the full code [here](../../kotlinx-coroutines-core/jvm/test/guide/example-supervision-01.kt).
>
Expand Down Expand Up @@ -418,6 +421,7 @@ import kotlin.coroutines.*
import kotlinx.coroutines.*

fun main() = runBlocking {
//sampleStart
try {
supervisorScope {
val child = launch {
Expand All @@ -436,8 +440,10 @@ fun main() = runBlocking {
} catch(e: AssertionError) {
println("Caught an assertion error")
}
//sampleEnd
}
```
{kotlin-runnable="true" kotlin-min-compiler-version="1.3"}

> You can get the full code [here](../../kotlinx-coroutines-core/jvm/test/guide/example-supervision-02.kt).
>
Expand Down Expand Up @@ -468,6 +474,7 @@ import kotlin.coroutines.*
import kotlinx.coroutines.*

fun main() = runBlocking {
//sampleStart
val handler = CoroutineExceptionHandler { _, exception ->
println("CoroutineExceptionHandler got $exception")
}
Expand All @@ -479,8 +486,10 @@ fun main() = runBlocking {
println("The scope is completing")
}
println("The scope is completed")
//sampleEnd
}
```
{kotlin-runnable="true" kotlin-min-compiler-version="1.3"}

> You can get the full code [here](../../kotlinx-coroutines-core/jvm/test/guide/example-supervision-03.kt).
>
Expand Down

0 comments on commit 3dcc349

Please sign in to comment.