File tree Expand file tree Collapse file tree 3 files changed +41
-1
lines changed
main/kotlin/kotlinx/coroutines/experimental
test/kotlin/kotlinx/coroutines/experimental Expand file tree Collapse file tree 3 files changed +41
-1
lines changed Original file line number Diff line number Diff line change @@ -100,6 +100,7 @@ Select expression to perform multiple suspending operations simultaneously until
100
100
[ suspendCancellableCoroutine ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/suspend-cancellable-coroutine.html
101
101
[ NonCancellable ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-non-cancellable/index.html
102
102
[ newCoroutineContext ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/new-coroutine-context.html
103
+ [ CoroutineExceptionHandler ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-exception-handler/index.html
103
104
<!-- - INDEX kotlinx.coroutines.experimental.sync -->
104
105
[ kotlinx.coroutines.experimental.sync.Mutex ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.sync/-mutex/index.html
105
106
[ kotlinx.coroutines.experimental.sync.Mutex.lock ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.sync/-mutex/lock.html
Original file line number Diff line number Diff line change 16
16
17
17
package kotlinx.coroutines.experimental
18
18
19
+ import kotlin.coroutines.experimental.AbstractCoroutineContextElement
19
20
import kotlin.coroutines.experimental.CoroutineContext
20
21
21
22
@@ -51,7 +52,17 @@ public interface CoroutineExceptionHandler : CoroutineContext.Element {
51
52
/* *
52
53
* Key for [CoroutineExceptionHandler] instance in the coroutine context.
53
54
*/
54
- companion object Key : CoroutineContext.Key<CoroutineExceptionHandler>
55
+ companion object Key : CoroutineContext.Key<CoroutineExceptionHandler> {
56
+ /* *
57
+ * Creates new [CoroutineExceptionHandler] instance.
58
+ * @param handler a function which handles exception thrown by a coroutine
59
+ */
60
+ public operator inline fun invoke (crossinline handler : (CoroutineContext , Throwable ) -> Unit ): CoroutineExceptionHandler =
61
+ object : AbstractCoroutineContextElement (Key ), CoroutineExceptionHandler {
62
+ override fun handleException (context : CoroutineContext , exception : Throwable ) =
63
+ handler.invoke(context, exception)
64
+ }
65
+ }
55
66
56
67
/* *
57
68
* Handles uncaught [exception] in the given [context]. It is invoked
Original file line number Diff line number Diff line change
1
+ package kotlinx.coroutines.experimental
2
+
3
+ import org.junit.Test
4
+ import java.util.concurrent.CountDownLatch
5
+ import java.util.concurrent.TimeUnit
6
+
7
+ class CoroutineExceptionHandlerTest {
8
+ @Test
9
+ fun testCoroutineExceptionHandlerCreator () {
10
+ val latch = CountDownLatch (1 )
11
+ var coroutineException: Throwable ? = null
12
+
13
+ val handler = CoroutineExceptionHandler { _, ex ->
14
+ coroutineException = ex
15
+ latch.countDown()
16
+ }
17
+
18
+ launch(CommonPool + handler) {
19
+ throw TestException ()
20
+ }
21
+
22
+ latch.await(10 , TimeUnit .SECONDS )
23
+
24
+ check(coroutineException is TestException )
25
+ }
26
+ }
27
+
28
+ private class TestException : RuntimeException ()
You can’t perform that action at this time.
0 commit comments