Skip to content

Commit 563da40

Browse files
committed
Stylistic updates to exception handling rework
1 parent 590696d commit 563da40

File tree

11 files changed

+241
-288
lines changed

11 files changed

+241
-288
lines changed

common/kotlinx-coroutines-core-common/src/CoroutineExceptionHandler.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,21 @@ internal expect fun handleCoroutineExceptionImpl(context: CoroutineContext, exce
2525
public fun handleCoroutineException(context: CoroutineContext, exception: Throwable, caller: Job? = null) {
2626
// if exception handling fails, make sure the original exception is not lost
2727
try {
28-
2928
// Ignore CancellationException (they are normal ways to terminate a coroutine)
3029
if (exception is CancellationException) {
3130
return
3231
}
33-
3432
// If parent is successfully cancelled, we're done, it is now its responsibility to handle the exception
3533
val parent = context[Job]
3634
// E.g. actor registers itself in the context, in that case we should invoke handler
3735
if (parent !== null && parent !== caller && parent.cancel(exception)) {
3836
return
3937
}
40-
4138
// If not, invoke exception handler from the context
4239
context[CoroutineExceptionHandler]?.let {
4340
it.handleException(context, exception)
4441
return
4542
}
46-
4743
// If handler is not present in the context, fallback to the global handler
4844
handleCoroutineExceptionImpl(context, exception)
4945
} catch (handlerException: Throwable) {

common/kotlinx-coroutines-core-common/src/Job.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public interface Job : CoroutineContext.Element {
154154
* or it's being cancelled and given [cause] was successfully received by the job and will be properly handled, `false` otherwise.
155155
*
156156
* If this method returned `false`, then caller is responsible for handling [cause].
157-
* If job is already completed, method returns `false`
157+
* If job is already completed, method returns `false`.
158158
*
159159
* When cancellation has a clear reason in the code, an instance of [CancellationException] should be created
160160
* at the corresponding original cancellation site and passed into this method to aid in debugging by providing

common/kotlinx-coroutines-core-common/src/JobSupport.kt

Lines changed: 81 additions & 101 deletions
Large diffs are not rendered by default.

core/kotlinx-coroutines-core/test/guide/example-exceptions-01.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,12 @@ fun main(args: Array<String>) = runBlocking {
1212
println("Throwing exception from launch")
1313
throw IndexOutOfBoundsException() // Will be printed to the console by Thread.defaultUncaughtExceptionHandler
1414
}
15-
1615
job.join()
1716
println("Joined failed job")
18-
1917
val deferred = async {
2018
println("Throwing exception from async")
2119
throw ArithmeticException() // Nothing is printed, relying on user to call await
2220
}
23-
2421
try {
2522
deferred.await()
2623
println("Unreached")

core/kotlinx-coroutines-core/test/guide/example-exceptions-02.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ package kotlinx.coroutines.experimental.guide.exceptions02
88
import kotlinx.coroutines.experimental.*
99

1010
fun main(args: Array<String>) = runBlocking {
11-
val handler = CoroutineExceptionHandler { _, exception -> println("Caught $exception") }
12-
11+
val handler = CoroutineExceptionHandler { _, exception ->
12+
println("Caught $exception")
13+
}
1314
val job = launch(handler) {
1415
throw AssertionError()
1516
}
16-
1717
val deferred = async(handler) {
1818
throw ArithmeticException() // Nothing will be printed, relying on user to call deferred.await()
1919
}
20-
2120
joinAll(job, deferred)
2221
}

core/kotlinx-coroutines-core/test/guide/example-exceptions-03.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ fun main(args: Array<String>) = runBlocking {
1717
println("Child is cancelled")
1818
}
1919
}
20-
2120
yield()
2221
println("Cancelling child")
2322
child.cancel()
2423
child.join()
2524
yield()
2625
println("Parent is not cancelled")
2726
}
28-
2927
job.join()
3028
}

core/kotlinx-coroutines-core/test/guide/example-exceptions-04.kt

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,26 @@ import kotlinx.coroutines.experimental.*
99
import kotlin.coroutines.experimental.*
1010

1111
fun main(args: Array<String>) = runBlocking {
12-
val handler = CoroutineExceptionHandler { _, exception -> println("Caught $exception") }
13-
14-
val job = launch(handler) {
15-
val child1 = launch(coroutineContext, start = CoroutineStart.ATOMIC) {
16-
try {
17-
delay(Long.MAX_VALUE)
18-
} finally {
19-
withContext(NonCancellable) {
20-
println("Children are cancelled, but exception is not handled until children are terminated completely")
21-
delay(100)
22-
println("Last child finished its non cancellable block")
23-
}
24-
}
25-
}
26-
27-
val child2 = launch(coroutineContext, start = CoroutineStart.ATOMIC) {
28-
delay(10)
29-
println("Child throws an exception")
30-
throw ArithmeticException()
31-
}
32-
}
33-
34-
job.join()
12+
val handler = CoroutineExceptionHandler { _, exception ->
13+
println("Caught $exception")
14+
}
15+
val job = launch(handler) {
16+
val child1 = launch(coroutineContext, start = CoroutineStart.ATOMIC) {
17+
try {
18+
delay(Long.MAX_VALUE)
19+
} finally {
20+
withContext(NonCancellable) {
21+
println("Children are cancelled, but exception is not handled until all children terminate")
22+
delay(100)
23+
println("Last child finished its non cancellable block")
24+
}
25+
}
26+
}
27+
val child2 = launch(coroutineContext, start = CoroutineStart.ATOMIC) {
28+
delay(10)
29+
println("Second child throws an exception")
30+
throw ArithmeticException()
31+
}
32+
}
33+
job.join()
3534
}

core/kotlinx-coroutines-core/test/guide/example-exceptions-05.kt

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,25 @@ package kotlinx.coroutines.experimental.guide.exceptions05
77

88
import kotlinx.coroutines.experimental.*
99
import kotlinx.coroutines.experimental.exceptions.*
10-
import java.io.*
1110
import kotlin.coroutines.experimental.*
11+
import java.io.*
1212

1313
fun main(args: Array<String>) = runBlocking {
1414
val handler = CoroutineExceptionHandler { _, exception ->
15-
println("Caught $exception with suppressed ${exception.suppressed().contentToString()}")
16-
}
17-
18-
val job = launch(handler + coroutineContext, parent = Job()) {
19-
launch(coroutineContext, start = CoroutineStart.ATOMIC) {
20-
try {
21-
delay(Long.MAX_VALUE)
22-
} finally {
23-
throw ArithmeticException()
24-
}
25-
}
26-
27-
launch(coroutineContext) {
28-
throw IOException()
29-
}
30-
31-
delay(Long.MAX_VALUE)
32-
}
33-
34-
job.join()
15+
println("Caught $exception with suppressed ${exception.suppressed().contentToString()}")
16+
}
17+
val job = launch(handler + coroutineContext, parent = Job()) {
18+
launch(coroutineContext, start = CoroutineStart.ATOMIC) {
19+
try {
20+
delay(Long.MAX_VALUE)
21+
} finally {
22+
throw ArithmeticException()
23+
}
24+
}
25+
launch(coroutineContext) {
26+
throw IOException()
27+
}
28+
delay(Long.MAX_VALUE)
29+
}
30+
job.join()
3531
}

core/kotlinx-coroutines-core/test/guide/example-exceptions-06.kt

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,27 @@
66
package kotlinx.coroutines.experimental.guide.exceptions06
77

88
import kotlinx.coroutines.experimental.*
9-
import java.io.*
109
import kotlin.coroutines.experimental.*
10+
import java.io.*
1111

1212
fun main(args: Array<String>) = runBlocking {
13-
val handler = CoroutineExceptionHandler { _, exception ->
14-
println("Caught original $exception")
15-
}
16-
17-
val job = launch(handler) {
18-
val inner = launch(coroutineContext) {
19-
launch(coroutineContext) {
20-
launch(coroutineContext) {
21-
throw IOException()
22-
}
23-
}
24-
}
25-
26-
try {
27-
inner.join()
28-
} catch (e: JobCancellationException) {
29-
println("Rethrowing JobCancellationException with original cause")
30-
throw e
31-
}
32-
}
33-
34-
job.join()
13+
val handler = CoroutineExceptionHandler { _, exception ->
14+
println("Caught original $exception")
15+
}
16+
val job = launch(handler) {
17+
val inner = launch(coroutineContext) {
18+
launch(coroutineContext) {
19+
launch(coroutineContext) {
20+
throw IOException()
21+
}
22+
}
23+
}
24+
try {
25+
inner.join()
26+
} catch (e: JobCancellationException) {
27+
println("Rethrowing JobCancellationException with original cause")
28+
throw e
29+
}
30+
}
31+
job.join()
3532
}

core/kotlinx-coroutines-core/test/guide/test/GuideTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit.
22
package kotlinx.coroutines.experimental.guide.test
33

4-
import org.junit.*
4+
import org.junit.Test
55

66
class GuideTest {
77

@@ -297,8 +297,8 @@ class GuideTest {
297297
@Test
298298
fun testKotlinxCoroutinesExperimentalGuideExceptions04() {
299299
test("KotlinxCoroutinesExperimentalGuideExceptions04") { kotlinx.coroutines.experimental.guide.exceptions04.main(emptyArray()) }.verifyLines(
300-
"Child throws an exception",
301-
"Children are cancelled, but exception is not handled until children are terminated completely",
300+
"Second child throws an exception",
301+
"Children are cancelled, but exception is not handled until all children terminate",
302302
"Last child finished its non cancellable block",
303303
"Caught java.lang.ArithmeticException"
304304
)

0 commit comments

Comments
 (0)