Catched exception still throwing

Hi! I must be missing something, i got this function:

fun copyToClipboard(image: BufferedImage) {
    try {
        ImageSelection(image).let {
            Toolkit.getDefaultToolkit().systemClipboard.setContents(it, null)
  "Copied Image to clipboard")
    } catch (exception: Exception) {
        CCLogger.warn("ImageUtils.copyToClipboard encountered an IOException error while trying to copy the image. Message: ${exception.message}")
        CCLogger.warn("This is generally harmless, it can be caused by your jvm not supporting jpgs apparently :^)")

Tldr: setContents sometimes throws an IIOException but it still works. So i want to log it, but not have it throw a full blown exception.
For some reason this does not do the job and the exception is still thrown.
What am i missing? Thanks!

A try catch block catches the thrown exception, it does not prevent the exception from being thrown. Do you mean the try catch block appears to fail to catch the thrown exception?

Yeah that, sorry!
Basically, id like there to not be the exception text in the console, instead as seen in the code i want my logger to warn the user that this happend. tldr: because the code that causes the exception still works, just not completly (trying to copy image into clipboard, but alpha channel is lost → exception).
Hope that makes sense. For some reason though i cant get this to work.
The code in the catch brackets is not executed even though an exception occurs inside the try brackets.

Are you sure the exception is thrown from setContents() and copyToClipboard()? Could you share the error message and the stacktrace?

1 Like

Gladly, this is the stacktrace:

javax.imageio.IIOException: Invalid argument to native writeImage
	at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeImage(Native Method)
	at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeOnThread(
	at com.sun.imageio.plugins.jpeg.JPEGImageWriter.write(
	at javax.imageio.ImageWriter.write(
	at sun.awt.datatransfer.DataTransferer.imageToStandardBytesImpl(
	at sun.awt.datatransfer.DataTransferer.imageToStandardBytes(
	at sun.awt.datatransfer.DataTransferer.translateTransferable(
	at sun.awt.datatransfer.SunClipboard.setContents(
	at net.snipsniper.utils.ImageUtils$Companion.copyToClipboard(ImageUtils.kt:37)
	at net.snipsniper.utils.ExtensionsKt.copyToClipboard(Extensions.kt:16)
	at net.snipsniper.capturewindow.CaptureWindow.capture(CaptureWindow.kt:163)
	at net.snipsniper.capturewindow.CaptureWindowListener.mouseReleased(CaptureWindowListener.kt:77)
	at java.awt.Component.processMouseEvent(
	at java.awt.Component.processEvent(
	at java.awt.Container.processEvent(
	at java.awt.Window.processEvent(
	at java.awt.Component.dispatchEventImpl(
	at java.awt.Container.dispatchEventImpl(
	at java.awt.Window.dispatchEventImpl(
	at java.awt.Component.dispatchEvent(
	at java.awt.EventQueue.dispatchEventImpl(
	at java.awt.EventQueue.access$500(
	at java.awt.EventQueue$
	at java.awt.EventQueue$
	at Method)
	at java.awt.EventQueue$
	at java.awt.EventQueue$
	at Method)
	at java.awt.EventQueue.dispatchEvent(
	at java.awt.EventDispatchThread.pumpOneEventForFilters(
	at java.awt.EventDispatchThread.pumpEventsForFilter(
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(
	at java.awt.EventDispatchThread.pumpEvents(
	at java.awt.EventDispatchThread.pumpEvents(

Now, as far as i understand it gets thrown here:

	at net.snipsniper.utils.ImageUtils$Companion.copyToClipboard(ImageUtils.kt:37)

Line 37 in this case is

Toolkit.getDefaultToolkit().systemClipboard.setContents(it, null)

So im at a loss, but maybe im reading this wrong :slight_smile:

Edit: Oh yeah, probably good to note, i noticed this only gets thrown when im running it with jdk 1.8.0_352 and not with jdk 19. But id like to catch this either way just to add a more fitting message :slight_smile:

I might have missed something but I can’t seem to find the methods called inside the try block from the stack trace. Perhaps the exception was thrown else where outside the try block.

It is there:

at sun.awt.datatransfer.SunClipboard.setContents(
at net.snipsniper.utils.ImageUtils$Companion.copyToClipboard(ImageUtils.kt:37)

@SvenWollinger This is indeed very strange. The only thing I can think of is that you have a different Exception class and you use it here. You can also try to catch Throwable, although Exception should catch it.

I was thinking that! Because it appears to be an exception class originating from the javafx package, ill look around a little more, maybe i need to catch something else. Thanks!

Its in the line with
at net.snipsniper.utils.ImageUtils$Companion.copyToClipboard(ImageUtils.kt:37)
which contains the code from my initial reply. its odd

Is it possibly being printed and rethrown by a method deeper in the stack?

1 Like

The messages i want to print with my custom Logger there arent being printed, that im sure off.
Not sure where else the exception would be thrown from sadly

First, using the runDebug Gradle task of your project, the message is printed, albeit as white text on a white background (at least in my terminal, in the image I have selected part of the text):

And second, that message and stacktrace gets printed by the JVM (I ran it with Temurin 17.0.5) via e.printStackTrace(). A recent commit removed that part. According to the tags listed there, that change seems to be in JDK 19+.

So there’s nothing you can do about it. It’s silenced in a newer JDK.

Happy further coding in Kotlin! :slight_smile:


Wow you went the extra mile, thanks!!
Regarding the color, im using dark mode and didnt think of also coloring the background, thats probably something i should look into, thanks :smiley:

I see, thank you so much for clearing that up. I wasnt too bothered by the exception itself, since that call still goes through after all, i just wanted to have an extra little message saying “Hi! Theres an exception, but you can probably ignore it!”, seems that wont be possible. Oh well!

Thanks again!!