File tree 2 files changed +29
-1
lines changed
core/kotlinx-coroutines-core
2 files changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -35,7 +35,7 @@ internal fun <E : Throwable> tryCopyException(exception: E): E? {
35
35
ctor = { e -> runCatching { constructor .newInstance(e) as E }.getOrNull() }
36
36
break
37
37
} else if (parameters.isEmpty()) {
38
- ctor = { e -> runCatching { constructor .newInstance() as E }.getOrNull()? .also { it.initCause(e) } }
38
+ ctor = { e -> runCatching { ( constructor .newInstance() as E ) .also { it.initCause(e) } }.getOrNull() }
39
39
break
40
40
}
41
41
}
Original file line number Diff line number Diff line change @@ -188,6 +188,34 @@ class StackTraceRecoveryTest : TestBase() {
188
188
deferred.join()
189
189
}
190
190
191
+ public class TrickyException () : Throwable() {
192
+ // To be sure ctor is never invoked
193
+ @Suppress(" UNUSED" , " UNUSED_PARAMETER" )
194
+ private constructor (message: String , cause: Throwable ): this () {
195
+ error(" Should never be called" )
196
+ }
197
+
198
+ override fun initCause (cause : Throwable ? ): Throwable {
199
+ error(" Can't call initCause" )
200
+ }
201
+ }
202
+
203
+ @Test
204
+ fun testThrowingInitCause () = runTest {
205
+ val deferred = async(NonCancellable ) {
206
+ expect(2 )
207
+ throw TrickyException ()
208
+ }
209
+
210
+ try {
211
+ expect(1 )
212
+ deferred.await()
213
+ } catch (e: TrickyException ) {
214
+ assertNull(e.cause)
215
+ finish(3 )
216
+ }
217
+ }
218
+
191
219
private suspend fun outerScopedMethod (deferred : Deferred <Nothing >, vararg traces : String ) = coroutineScope {
192
220
innerMethod(deferred, * traces)
193
221
assertTrue(true )
You can’t perform that action at this time.
0 commit comments