Javaru

Javaru

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

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

Marked As Solved

sam-cooper

sam-cooper

Author of Kotlin Coroutine Confidence

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.

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.

Where Next?

Popular Pragmatic Bookshelf topics Top

edruder
I thought that there might be interest in using the book with Rails 6.1 and Ruby 2.7.2. I’ll note what I needed to do differently here. ...
New
Mmm
Hi, build fails on: bracket-lib = “~0.8.1” when running on Mac Mini M1 Rust version 1.5.0: Compiling winit v0.22.2 error[E0308]: mi...
New
jeremyhuiskamp
Title: Web Development with Clojure, Third Edition, vB17.0 (p9) The create table guestbook syntax suggested doesn’t seem to be accepted ...
New
AndyDavis3416
@noelrappin Running the webpack dev server, I receive the following warning: ERROR in tsconfig.json TS18003: No inputs were found in c...
New
brunogirin
When I run the coverage example to report on missing lines, I get: pytest --cov=cards --report=term-missing ch7 ERROR: usage: pytest [op...
New
Charles
In general, the book isn’t yet updated for Phoenix version 1.6. On page 18 of the book, the authors indicate that an auto generated of ro...
New
brunogirin
When trying to run tox in parallel as explained on page 151, I got the following error: tox: error: argument -p/–parallel: expected one...
New
adamwoolhether
Is there any place where we can discuss the solutions to some of the exercises? I can figure most of them out, but am having trouble with...
New
SlowburnAZ
Getting an error when installing the dependencies at the start of this chapter: could not compile dependency :exla, "mix compile" failed...
New
dachristenson
@mfazio23 Android Studio will not accept anything I do when trying to use the Transformations class, as described on pp. 140-141. Googl...
New

Other popular topics Top

AstonJ
I’ve been hearing quite a lot of comments relating to the sound of a keyboard, with one of the most desirable of these called ‘thock’, he...
New
PragmaticBookshelf
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
New
AstonJ
This looks like a stunning keycap set :orange_heart: A LEGENDARY KEYBOARD LIVES ON When you bought an Apple Macintosh computer in the e...
New
Exadra37
I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
New
New
wmnnd
Here’s the story how one of the world’s first production deployments of LiveView came to be - and how trying to improve it almost caused ...
New
gagan7995
API 4 Path: /user/following/ Method: GET Description: Returns the list of all names of people whom the user follows Response [ { ...
New
husaindevelop
Inside our android webview app, we are trying to paste the copied content from another app eg (notes) using navigator.clipboard.readtext ...
New
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
New
AstonJ
This is a very quick guide, you just need to: Download LM Studio: https://lmstudio.ai/ Click on search Type DeepSeek, then select the o...
New

Sub Categories: