
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.
Marked As Solved

sam-cooper
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









Modern Front-End Development for Rails - application does not start after run bin/setup (page xviii)

Other popular topics










Latest in Kotlin Coroutine Confidence
Latest in PragProg Customers
Latest (all)
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /rails
- /js
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /haskell
- /emacs
- /svelte
- /onivim
- /typescript
- /crystal
- /c-plus-plus
- /tailwind
- /kotlin
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /vscode
- /opensuse
- /ash
- /centos
- /php
- /deepseek
- /scala
- /html
- /zig
- /debian
- /nixos
- /lisp
- /agda
- /textmate
- /react-native
- /sublime-text
- /kubuntu
- /arch-linux
- /revery
- /ubuntu
- /manjaro
- /spring
- /django
- /diversity
- /nodejs
- /lua
- /julia
- /c
- /slackware
- /neovim