And Exception

package com.glassthought.sandbox

import gt.sandbox.util.output.Out
import kotlinx.coroutines.*

val out = Out.standard()

fun main() = runBlocking {
  // Start two async operations in parallel
  val deferredOne = async { fetchDataOne() }
  val deferredTwo = async { fetchDataTwo() }

  // Wait for results

  try {
    val resultOne = deferredOne.await()
    out.println("Result one: $resultOne")
    val resultTwo = deferredTwo.await()
    out.println("Combined result: ${resultOne + resultTwo}")
  } catch (e: Exception) {

    out.printlnRed("Caught exception on main thread: ${e.message}")
  }
}

// Simulated data fetch function
suspend fun fetchDataOne(): Int {
  delay(2000)  // Simulate a network or heavy computation delay


  out.println("Fetched data one, but throwing exception")
  throw RuntimeException("Failed to fetch data one")
  return 10
}

// Another simulated data fetch function
suspend fun fetchDataTwo(): Int {
  delay(1500)  // Simulate a network or heavy computation delay
  out.println("Fetched data two")
  return 20
}
Snapshot & output

Command to reproduce:

gt.sandbox.checkout.commit 645cfc96d490005817d4 \
&& cd "${GT_SANDBOX_REPO}" \
&& cmd.run.announce "./gradlew run"

Recorded output of command:

> Task :app:checkKotlinGradlePluginConfigurationErrors SKIPPED
> Task :app:compileKotlin UP-TO-DATE
> Task :app:compileJava NO-SOURCE
> Task :app:processResources NO-SOURCE
> Task :app:classes UP-TO-DATE

> Task :app:run
[2024-11-13T05:11:42.594110Z][elapsed-since-start: 1561ms][tname:main/tid:1] Fetched data two
[2024-11-13T05:11:43.093943Z][elapsed-since-start: 2033ms][tname:main/tid:1] Fetched data one, but throwing exception
[2024-11-13T05:11:43.101074Z][elapsed-since-start: 2040ms][tname:main/tid:1] Caught exception on main thread: Failed to fetch data one
Exception in thread "main" java.lang.RuntimeException: Failed to fetch data one
	at com.glassthought.sandbox.MainKt.fetchDataOne(Main.kt:32)
	at com.glassthought.sandbox.MainKt$fetchDataOne$1.invokeSuspend(Main.kt)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTaskKt.resume(DispatchedTask.kt:235)
	at kotlinx.coroutines.DispatchedTaskKt.dispatch(DispatchedTask.kt:168)
	at kotlinx.coroutines.CancellableContinuationImpl.dispatchResume(CancellableContinuationImpl.kt:474)
	at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl(CancellableContinuationImpl.kt:508)
	at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl$default(CancellableContinuationImpl.kt:497)
	at kotlinx.coroutines.CancellableContinuationImpl.resumeUndispatched(CancellableContinuationImpl.kt:595)
	at kotlinx.coroutines.EventLoopImplBase$DelayedResumeTask.run(EventLoop.common.kt:493)
	at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:280)
	at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:85)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:59)
	at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:38)
	at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
	at com.glassthought.sandbox.MainKt.main(Main.kt:8)
	at com.glassthought.sandbox.MainKt.main(Main.kt)

> Task :app:run FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:run'.
> Process 'command '/Users/nickolaykondratyev/Library/Java/JavaVirtualMachines/corretto-21.0.3/Contents/Home/bin/java'' finished with non-zero exit value 1

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.

BUILD FAILED in 2s
2 actionable tasks: 1 executed, 1 up-to-date

#question (Private): Why did stack trace get printed even though I caught the exception?