Make examples runnable
* Also drop args from fun main
diff --git a/docs/exception-handling.md b/docs/exception-handling.md
index d50fba0..9e5334d 100644
--- a/docs/exception-handling.md
+++ b/docs/exception-handling.md
@@ -5,8 +5,6 @@
// This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit.
package kotlinx.coroutines.guide.$$1$$2
-
-import kotlinx.coroutines.*
-->
<!--- KNIT ../core/kotlinx-coroutines-core/test/guide/.*\.kt -->
<!--- TEST_OUT ../core/kotlinx-coroutines-core/test/guide/test/ExceptionsGuideTest.kt
@@ -35,8 +33,6 @@
## Exception handling
-<!--- INCLUDE .*/example-exceptions-([0-9]+).kt
--->
This section covers exception handling and cancellation on exceptions.
We already know that cancelled coroutine throws [CancellationException] in suspension points and that it
@@ -57,7 +53,9 @@
<div class="sample" markdown="1" theme="idea" data-highlight-only>
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking {
val job = GlobalScope.launch {
println("Throwing exception from launch")
throw IndexOutOfBoundsException() // Will be printed to the console by Thread.defaultUncaughtExceptionHandler
@@ -109,10 +107,13 @@
[CoroutineExceptionHandler] is invoked only on exceptions which are not expected to be handled by the user,
so registering it in [async] builder and the like of it has no effect.
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking {
+//sampleStart
val handler = CoroutineExceptionHandler { _, exception ->
println("Caught $exception")
}
@@ -123,6 +124,7 @@
throw ArithmeticException() // Nothing will be printed, relying on user to call deferred.await()
}
joinAll(job, deferred)
+//sampleEnd
}
```
@@ -146,14 +148,13 @@
When a coroutine is cancelled using [Job.cancel] without a cause, it terminates, but it does not cancel its parent.
Cancelling without cause is a mechanism for parent to cancel its children without cancelling itself.
-<!--- INCLUDE
-import kotlin.coroutines.*
--->
-
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking {
+//sampleStart
val job = launch {
val child = launch {
try {
@@ -170,6 +171,7 @@
println("Parent is not cancelled")
}
job.join()
+//sampleEnd
}
```
@@ -198,14 +200,13 @@
is launched in the scope of the main [runBlocking], since the main coroutine is going to be always cancelled
when its child completes with exception despite the installed handler.
-<!--- INCLUDE
-import kotlin.coroutines.*
--->
-
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking {
+//sampleEnd
val handler = CoroutineExceptionHandler { _, exception ->
println("Caught $exception")
}
@@ -228,6 +229,7 @@
}
}
job.join()
+//sampleEnd
}
```
@@ -257,16 +259,19 @@
would cause implementation details of a coroutines (whether it had delegated parts of its work to its children or not)
to leak to its exception handler.
+
<!--- INCLUDE
+
import kotlinx.coroutines.exceptions.*
-import kotlin.coroutines.*
-import java.io.*
-->
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+import java.io.*
+
+fun main() = runBlocking {
val handler = CoroutineExceptionHandler { _, exception ->
println("Caught $exception with suppressed ${exception.suppressed.contentToString()}")
}
@@ -284,7 +289,7 @@
}
delay(Long.MAX_VALUE)
}
- job.join()
+ job.join()
}
```
@@ -307,15 +312,14 @@
Cancellation exceptions are transparent and unwrapped by default:
-<!--- INCLUDE
-import kotlin.coroutines.*
-import java.io.*
--->
-
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+import java.io.*
+
+fun main() = runBlocking {
+//sampleStart
val handler = CoroutineExceptionHandler { _, exception ->
println("Caught original $exception")
}
@@ -335,6 +339,7 @@
}
}
job.join()
+//sampleEnd
}
```
@@ -367,14 +372,12 @@
For these purposes [SupervisorJob][SupervisorJob()] can be used. It is similar to a regular [Job][Job()] with the only exception that cancellation is propagated
only downwards. It is easy to demonstrate with an example:
-<!--- INCLUDE
-import kotlin.coroutines.*
--->
-
<div class="sample" markdown="1" theme="idea" data-highlight-only>
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking {
val supervisor = SupervisorJob()
with(CoroutineScope(coroutineContext + supervisor)) {
// launch the first child -- its exception is ignored for this example (don't do this in practice!)
@@ -424,14 +427,13 @@
only in one direction and cancels all children only if it has failed itself. It also waits for all children before completion
just like [coroutineScope] does.
-<!--- INCLUDE
-import kotlin.coroutines.*
--->
-
<div class="sample" markdown="1" theme="idea" data-highlight-only>
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlin.coroutines.*
+import kotlinx.coroutines.*
+
+fun main() = runBlocking {
try {
supervisorScope {
val child = launch {
@@ -473,14 +475,13 @@
Every child should handle its exceptions by itself via exception handling mechanisms.
This difference comes from the fact that child's failure is not propagated to the parent.
-<!--- INCLUDE
-import kotlin.coroutines.*
--->
-
<div class="sample" markdown="1" theme="idea" data-highlight-only>
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlin.coroutines.*
+import kotlinx.coroutines.*
+
+fun main() = runBlocking {
val handler = CoroutineExceptionHandler { _, exception ->
println("Caught $exception")
}