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
100100[ suspendCancellableCoroutine ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/suspend-cancellable-coroutine.html
101101[ NonCancellable ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-non-cancellable/index.html
102102[ 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
103104<!-- - INDEX kotlinx.coroutines.experimental.sync -->
104105[ kotlinx.coroutines.experimental.sync.Mutex ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.sync/-mutex/index.html
105106[ 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 1616
1717package kotlinx.coroutines.experimental
1818
19+ import kotlin.coroutines.experimental.AbstractCoroutineContextElement
1920import kotlin.coroutines.experimental.CoroutineContext
2021
2122
@@ -51,7 +52,17 @@ public interface CoroutineExceptionHandler : CoroutineContext.Element {
5152 /* *
5253 * Key for [CoroutineExceptionHandler] instance in the coroutine context.
5354 */
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+ }
5566
5667 /* *
5768 * 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