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.

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
JohnS
I can’t setup the Rails source code. This happens in a working directory containing multiple (postgres) Rails apps. With: ruby-3.0.0 s...
New
joepstender
The generated iex result below should list products instead of product for the metadata. (page 67) iex> product = %Product{} %Pento....
New
jskubick
I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
New
digitalbias
Title: Build a Weather Station with Elixir and Nerves: Problem connecting to Postgres with Grafana on (page 64) If you follow the defau...
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
New
s2k
Hi all, currently I wonder how the Tailwind colours work (or don’t work). For example, in app/views/layouts/application.html.erb I have...
New
tkhobbes
After some hassle, I was able to finally run bin/setup, now I have started the rails server but I get this error message right when I vis...
New
ggerico
I got this error when executing the plot files on macOS Ventura 13.0.1 with Python 3.10.8 and matplotlib 3.6.1: programming_ML/code/03_...
New

Other popular topics Top

New
AstonJ
poll poll Be sure to check out @Dusty’s article posted here: An Introduction to Alternative Keyboard Layouts It’s one of the best write-...
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
rustkas
Intensively researching Erlang books and additional resources on it, I have found that the topic of using Regular Expressions is either c...
New
mafinar
This is going to be a long an frequently posted thread. While talking to a friend of mine who has taken data structure and algorithm cou...
New
First poster: joeb
The File System Access API with Origin Private File System. WebKit supports new API that makes it possible for web apps to create, open,...
New
PragmaticBookshelf
Author Spotlight Jamis Buck @jamis This month, we have the pleasure of spotlighting author Jamis Buck, who has written Mazes for Prog...
New
New
CommunityNews
A Brief Review of the Minisforum V3 AMD Tablet. Update: I have created an awesome-minisforum-v3 GitHub repository to list information fo...
New
AstonJ
This is cool! DEEPSEEK-V3 ON M4 MAC: BLAZING FAST INFERENCE ON APPLE SILICON We just witnessed something incredible: the largest open-s...
New

Latest in Kotlin Coroutine Confidence