Basic CoroutineExceptionHandler implementation.
diff --git a/kotlinx-coroutines-core/README.md b/kotlinx-coroutines-core/README.md
index ee0c94e..39b6266 100644
--- a/kotlinx-coroutines-core/README.md
+++ b/kotlinx-coroutines-core/README.md
@@ -100,6 +100,7 @@
[suspendCancellableCoroutine]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/suspend-cancellable-coroutine.html
[NonCancellable]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-non-cancellable/index.html
[newCoroutineContext]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/new-coroutine-context.html
+[CoroutineExceptionHandler]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-exception-handler/index.html
<!--- INDEX kotlinx.coroutines.experimental.sync -->
[kotlinx.coroutines.experimental.sync.Mutex]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.sync/-mutex/index.html
[kotlinx.coroutines.experimental.sync.Mutex.lock]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.sync/-mutex/lock.html
diff --git a/kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/CoroutineExceptionHandler.kt b/kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/CoroutineExceptionHandler.kt
index 30add0a..2ebede3 100644
--- a/kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/CoroutineExceptionHandler.kt
+++ b/kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/CoroutineExceptionHandler.kt
@@ -16,6 +16,7 @@
package kotlinx.coroutines.experimental
+import kotlin.coroutines.experimental.AbstractCoroutineContextElement
import kotlin.coroutines.experimental.CoroutineContext
@@ -51,7 +52,17 @@
/**
* Key for [CoroutineExceptionHandler] instance in the coroutine context.
*/
- companion object Key : CoroutineContext.Key<CoroutineExceptionHandler>
+ companion object Key : CoroutineContext.Key<CoroutineExceptionHandler> {
+ /**
+ * Creates new [CoroutineExceptionHandler] instance.
+ * @param handler a function which handles exception thrown by a coroutine
+ */
+ public operator inline fun invoke(crossinline handler: (CoroutineContext, Throwable) -> Unit): CoroutineExceptionHandler =
+ object: AbstractCoroutineContextElement(Key), CoroutineExceptionHandler {
+ override fun handleException(context: CoroutineContext, exception: Throwable) =
+ handler.invoke(context, exception)
+ }
+ }
/**
* Handles uncaught [exception] in the given [context]. It is invoked
diff --git a/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/CoroutineExceptionHandlerTest.kt b/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/CoroutineExceptionHandlerTest.kt
new file mode 100644
index 0000000..1d48dd9
--- /dev/null
+++ b/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/CoroutineExceptionHandlerTest.kt
@@ -0,0 +1,28 @@
+package kotlinx.coroutines.experimental
+
+import org.junit.Test
+import java.util.concurrent.CountDownLatch
+import java.util.concurrent.TimeUnit
+
+class CoroutineExceptionHandlerTest {
+ @Test
+ fun testCoroutineExceptionHandlerCreator() {
+ val latch = CountDownLatch(1)
+ var coroutineException: Throwable? = null
+
+ val handler = CoroutineExceptionHandler { _, ex ->
+ coroutineException = ex
+ latch.countDown()
+ }
+
+ launch(CommonPool + handler) {
+ throw TestException()
+ }
+
+ latch.await(10, TimeUnit.SECONDS)
+
+ check(coroutineException is TestException)
+ }
+}
+
+private class TestException: RuntimeException()
\ No newline at end of file