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.
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.