diff --git a/build-common/src/org/jetbrains/kotlin/incremental/CompilationTransaction.kt b/build-common/src/org/jetbrains/kotlin/incremental/CompilationTransaction.kt index 809b96a1fb9..0b57b5e80a0 100644 --- a/build-common/src/org/jetbrains/kotlin/incremental/CompilationTransaction.kt +++ b/build-common/src/org/jetbrains/kotlin/incremental/CompilationTransaction.kt @@ -240,17 +240,21 @@ class RecoverableCompilationTransaction( override fun close() { val mainException = closeCachesManager() - - try { + val exceptionToThrow = runCatching { if (isSuccessful) { cleanupStash() } else { revertChanges() cleanupStash() } - } catch (t: Throwable) { - mainException?.addSuppressed(t) - throw mainException ?: t + }.exceptionOrNull().run { + if (this != null) { + mainException?.addSuppressed(this) + } + mainException ?: this + } + if (exceptionToThrow != null) { + throw exceptionToThrow } } }