Repository: kotlin/kotlinx.coroutines
Issue: Accessing 'stackTrace' on JobCancellationException instance leads to crash on Android 6.0 and below

Here is simple example:

```kotlin
runBlocking {
    val job = launch {
        try {
            delay(1000)
        } catch (e: Exception) {
            e.stackTrace // accessing 'stackTrace' lead to crash
        }
    }
    yield()
    job.cancelAndJoin()
}
```
When you run the code above on Android 6.0 and lower, application will crash with following exception:
```
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.Object.clone()' on a null object reference
```

The analogue issue is https://github.com/akaita/RxJava2Debug/issues/2

Due to the bug on Android 6.0 and below, when you override `fillInStackTrace` and don't call super method, under-the-hood the exception stack trace not filled by [nativeFillInStackTrace()](https://android.googlesource.com/platform/libcore.git/+/android-6.0.1_r81/luni/src/main/java/java/lang/Throwable.java#166) method, so next time when you try to call [getStackTrace()](https://android.googlesource.com/platform/libcore.git/+/android-6.0.1_r81/luni/src/main/java/java/lang/Throwable.java#199), under-the-hood the [nativeGetStackTrace()](https://android.googlesource.com/platform/libcore.git/+/android-6.0.1_r81/luni/src/main/java/java/lang/Throwable.java#264) method will return `null` and [clone()](https://android.googlesource.com/platform/libcore.git/+/android-6.0.1_r81/luni/src/main/java/java/lang/Throwable.java#200) will be called on that `null`.


**Solution**
To fix this issue you should set empty stack trace, when overriding `fillInStackTrace` method, like following:
```kotlin
override fun fillInStackTrace(): Throwable {
    stackTrace = emptyArray<StackTraceElement>()
    return this
}
```
Also to reduce garbage, this empty array can be defined as global constant.

This fix must be applied to all custom exceptions, those override `fillInStackTrace`, currently these are:
https://github.com/Kotlin/kotlinx.coroutines/blob/d7de5f5ba66a8d005e5cbd03b18522112303fd54/kotlinx-coroutines-core/jvm/src/Exceptions.kt#L29
https://github.com/Kotlin/kotlinx.coroutines/blob/d7de5f5ba66a8d005e5cbd03b18522112303fd54/kotlinx-coroutines-core/jvm/src/flow/internal/FlowExceptions.kt#L10
https://github.com/Kotlin/kotlinx.coroutines/blob/d7de5f5ba66a8d005e5cbd03b18522112303fd54/kotlinx-coroutines-core/jvm/src/flow/internal/FlowExceptions.kt#L20


**Workaround**
Enable [Debug Mode](https://github.com/Kotlin/kotlinx.coroutines/blob/master/docs/debugging.md#debug-mode), so that `fillInStackTrace` will work as expected.






Changed files:
kotlinx-coroutines-core/jvm/src/Exceptions.kt, kotlinx-coroutines-core/jvm/src/flow/internal/FlowExceptions.kt

diff --git a/kotlinx-coroutines-core/jvm/src/Exceptions.kt b/kotlinx-coroutines-core/jvm/src/Exceptions.kt
index 22a02a30b..0684ce239 100644
--- a/kotlinx-coroutines-core/jvm/src/Exceptions.kt
+++ b/kotlinx-coroutines-core/jvm/src/Exceptions.kt
@@ -40,7 +40,8 @@ internal actual class JobCancellationException public actual constructor(
         if (DEBUG) {
             return super.fillInStackTrace()
         }
-
+        // Prevent Android <= 6.0 bug, #1866
+        stackTrace = emptyArray()
         /*
          * In non-debug mode we don't want to have a stacktrace on every cancellation/close,
          * parent job reference is enough. Stacktrace of JCE is not needed most of the time (e.g., it is not logged)
diff --git a/kotlinx-coroutines-core/jvm/src/flow/internal/FlowExceptions.kt b/kotlinx-coroutines-core/jvm/src/flow/internal/FlowExceptions.kt
index 863c2cc4a..4fb958ac7 100644
--- a/kotlinx-coroutines-core/jvm/src/flow/internal/FlowExceptions.kt
+++ b/kotlinx-coroutines-core/jvm/src/flow/internal/FlowExceptions.kt
@@ -12,14 +12,18 @@ internal actual class AbortFlowException actual constructor(
 ) : CancellationException("Flow was aborted, no more elements needed") {
 
     override fun fillInStackTrace(): Throwable {
-        if (DEBUG) super.fillInStackTrace()
+        if (DEBUG) return super.fillInStackTrace()
+        // Prevent Android <= 6.0 bug, #1866
+        stackTrace = emptyArray()
         return this
     }
 }
 
 internal actual class ChildCancelledException : CancellationException("Child of the scoped flow was cancelled") {
     override fun fillInStackTrace(): Throwable {
-        if (DEBUG) super.fillInStackTrace()
+        if (DEBUG) return super.fillInStackTrace()
+        // Prevent Android <= 6.0 bug, #1866
+        stackTrace = emptyArray()
         return this
     }
 }