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)
            CCLogger.info("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(JPEGImageWriter.java:1067)
	at com.sun.imageio.plugins.jpeg.JPEGImageWriter.write(JPEGImageWriter.java:363)
	at javax.imageio.ImageWriter.write(ImageWriter.java:615)
	at sun.awt.datatransfer.DataTransferer.imageToStandardBytesImpl(DataTransferer.java:2215)
	at sun.awt.datatransfer.DataTransferer.imageToStandardBytes(DataTransferer.java:2145)
	at sun.awt.windows.WDataTransferer.imageToPlatformBytes(WDataTransferer.java:352)
	at sun.awt.datatransfer.DataTransferer.translateTransferable(DataTransferer.java:1223)
	at sun.awt.windows.WDataTransferer.translateTransferable(WDataTransferer.java:219)
	at sun.awt.windows.WClipboard.setContentsNative(WClipboard.java:83)
	at sun.awt.datatransfer.SunClipboard.setContents(SunClipboard.java:106)
	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(Component.java:6539)
	at java.awt.Component.processEvent(Component.java:6304)
	at java.awt.Container.processEvent(Container.java:2239)
	at java.awt.Window.processEvent(Window.java:2025)
	at java.awt.Component.dispatchEventImpl(Component.java:4889)
	at java.awt.Container.dispatchEventImpl(Container.java:2297)
	at java.awt.Window.dispatchEventImpl(Window.java:2746)
	at java.awt.Component.dispatchEvent(Component.java:4711)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
	at java.awt.EventQueue.access$500(EventQueue.java:97)
	at java.awt.EventQueue$3.run(EventQueue.java:709)
	at java.awt.EventQueue$3.run(EventQueue.java:703)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
	at java.awt.EventQueue$4.run(EventQueue.java:733)
	at java.awt.EventQueue$4.run(EventQueue.java:731)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

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(SunClipboard.java:106)
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 CCLogger.info 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:

2 Likes

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