# Kotlin Coroutine Confidence:  try-catch not working (pg 37)

**URL:** <https://forum.devtalk.com/t/kotlin-coroutine-confidence-try-catch-not-working-pg-37/196093>\
**Category:** PragProg Customers\
**Tags:** book-kotlin-coroutine-confidence, errata\
**Created:** [22 April 2025 21:47 UTC](https://forum.devtalk.com/t/kotlin-coroutine-confidence-try-catch-not-working-pg-37/196093 "2025-04-22T21:47:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Javaru](https://forum.devtalk.com/user_avatar/forum.devtalk.com/javaru/32/7352_2.png) [@Javaru](https://forum.devtalk.com/u/Javaru)\
**Post date:** [22 April 2025 21:47 UTC](https://forum.devtalk.com/t/kotlin-coroutine-confidence-try-catch-not-working-pg-37/196093/1 "2025-04-22T21:47:38Z")

</div>

For the example `astronomy/v3/src/main/kotlin/com/example/astronomy/AstronomyApplication.kt`, despite the statement “That try–catch block is working again” at the end of pg 37, when I change the URL to `https://example.com` in `AstronomyService.kt`, the Swing app hangs indefinitely with the “Loading your image…” message. Same if I use the correct URL but `INVALID_KEY` for the `apiKey`. The try-catch does not appear to be handling any errors. (There is no error or stacktrace written to the console either.)

This is in the B3 version of the book with the example code downloaded on 2025-04-19 at 14:42 EDT.

#book-kotlin-coroutine-confidence #pragprog

---

<div class="post-metadata">

**Author:** ![sam-cooper](https://forum.devtalk.com/user_avatar/forum.devtalk.com/sam-cooper/32/7446_2.png) [@sam-cooper](https://forum.devtalk.com/u/sam-cooper)\
**Post date:** [23 April 2025 09:24 UTC](https://forum.devtalk.com/t/kotlin-coroutine-confidence-try-catch-not-working-pg-37/196093/2 "2025-04-23T09:24:14Z")

</div>

Oops! Thank you for spotting this problem. I can see the same issue when I run the code myself. Something must have gone wrong when I was testing this part of the code.

I think the code catches the exception, but fails to update the window to display the error message text, so there’s no sign that an error was caught.

Here’s a modified version that should work better. I’ve changed the `catch` block to put the error message in a new dialog box of its own. Acknowledging the error will also close the app, so it doesn’t hang forever.

```kotlin
suspend fun main(): Unit = withContext(Dispatchers.Main) {
  val window = createImageViewerWindow()

  try {
    loadImage(window)
  } catch (error: Throwable) {
    JOptionPane.showMessageDialog(window, "Sorry, something went wrong")
    window.dispose()
  }
}

```

Thank you again for spotting my mistake! Please can you confirm that you do see an error message when using a bad URL or API key with this new version of the code? I’ll be sure to fix this code in the next Beta update.

---

<div class="post-metadata">

**Author:** ![Javaru](https://forum.devtalk.com/user_avatar/forum.devtalk.com/javaru/32/7352_2.png) [@Javaru](https://forum.devtalk.com/u/Javaru)\
**Post date:** [23 April 2025 13:38 UTC](https://forum.devtalk.com/t/kotlin-coroutine-confidence-try-catch-not-working-pg-37/196093/3 "2025-04-23T13:38:50Z")

</div>

Thanks. I can confirm that the change resolves the issue.
