@@ -103,6 +103,42 @@ job: I'm sleeping 4 ...
103
103
main: Now I can quit.
104
104
-->
105
105
106
+ The same problem can be observed by catching a [ CancellationException] and not rethrowing it:
107
+
108
+ ``` kotlin
109
+ import kotlinx.coroutines.*
110
+
111
+ fun main () = runBlocking {
112
+ // sampleStart
113
+ val job = launch(Dispatchers .Default ) {
114
+ repeat(5 ) { i ->
115
+ try {
116
+ // print a message twice a second
117
+ println (" job: I'm sleeping $i ..." )
118
+ delay(500 )
119
+ } catch (e: Exception ) {
120
+ // log the exception
121
+ println (e)
122
+ }
123
+ }
124
+ }
125
+ delay(1300L ) // delay a bit
126
+ println (" main: I'm tired of waiting!" )
127
+ job.cancelAndJoin() // cancels the job and waits for its completion
128
+ println (" main: Now I can quit." )
129
+ // sampleEnd
130
+ }
131
+ ```
132
+ {kotlin-runnable="true" kotlin-min-compiler-version="1.3"}
133
+
134
+ > You can get the full code [ here] ( ../../kotlinx-coroutines-core/jvm/test/guide/example-cancel-03.kt ) .
135
+ >
136
+ {type="note"}
137
+
138
+ While catching ` Exception ` is an anti-pattern, this issue may surface in more subtle ways, like when using the
139
+ [ ` runCatching ` ] ( https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/run-catching.html ) function,
140
+ which does not know rethrow [ CancellationException] .
141
+
106
142
## Making computation code cancellable
107
143
108
144
There are two approaches to making computation code cancellable. The first one is to periodically
@@ -137,7 +173,7 @@ fun main() = runBlocking {
137
173
```
138
174
{kotlin-runnable="true" kotlin-min-compiler-version="1.3"}
139
175
140
- > You can get the full code [ here] ( ../../kotlinx-coroutines-core/jvm/test/guide/example-cancel-03 .kt ) .
176
+ > You can get the full code [ here] ( ../../kotlinx-coroutines-core/jvm/test/guide/example-cancel-04 .kt ) .
141
177
>
142
178
{type="note"}
143
179
@@ -182,7 +218,7 @@ fun main() = runBlocking {
182
218
```
183
219
{kotlin-runnable="true" kotlin-min-compiler-version="1.3"}
184
220
185
- > You can get the full code [ here] ( ../../kotlinx-coroutines-core/jvm/test/guide/example-cancel-04 .kt ) .
221
+ > You can get the full code [ here] ( ../../kotlinx-coroutines-core/jvm/test/guide/example-cancel-05 .kt ) .
186
222
>
187
223
{type="note"}
188
224
@@ -237,7 +273,7 @@ fun main() = runBlocking {
237
273
```
238
274
{kotlin-runnable="true" kotlin-min-compiler-version="1.3"}
239
275
240
- > You can get the full code [ here] ( ../../kotlinx-coroutines-core/jvm/test/guide/example-cancel-05 .kt ) .
276
+ > You can get the full code [ here] ( ../../kotlinx-coroutines-core/jvm/test/guide/example-cancel-06 .kt ) .
241
277
>
242
278
{type="note"}
243
279
@@ -275,7 +311,7 @@ fun main() = runBlocking {
275
311
```
276
312
{kotlin-runnable="true" kotlin-min-compiler-version="1.3"}
277
313
278
- > You can get the full code [ here] ( ../../kotlinx-coroutines-core/jvm/test/guide/example-cancel-06 .kt ) .
314
+ > You can get the full code [ here] ( ../../kotlinx-coroutines-core/jvm/test/guide/example-cancel-07 .kt ) .
279
315
>
280
316
{type="note"}
281
317
@@ -318,7 +354,7 @@ fun main() = runBlocking {
318
354
```
319
355
{kotlin-runnable="true" kotlin-min-compiler-version="1.3"}
320
356
321
- > You can get the full code [ here] ( ../../kotlinx-coroutines-core/jvm/test/guide/example-cancel-07 .kt ) .
357
+ > You can get the full code [ here] ( ../../kotlinx-coroutines-core/jvm/test/guide/example-cancel-08 .kt ) .
322
358
>
323
359
{type="note"}
324
360
@@ -378,7 +414,7 @@ fun main() {
378
414
```
379
415
{kotlin-runnable="true" kotlin-min-compiler-version="1.3"}
380
416
381
- > You can get the full code [ here] ( ../../kotlinx-coroutines-core/jvm/test/guide/example-cancel-08 .kt ) .
417
+ > You can get the full code [ here] ( ../../kotlinx-coroutines-core/jvm/test/guide/example-cancel-09 .kt ) .
382
418
>
383
419
{type="note"}
384
420
@@ -431,7 +467,7 @@ fun main() {
431
467
```
432
468
{kotlin-runnable="true" kotlin-min-compiler-version="1.3"}
433
469
434
- > You can get the full code [ here] ( ../../kotlinx-coroutines-core/jvm/test/guide/example-cancel-09 .kt ) .
470
+ > You can get the full code [ here] ( ../../kotlinx-coroutines-core/jvm/test/guide/example-cancel-10 .kt ) .
435
471
>
436
472
{type="note"}
437
473
0 commit comments