manuelbl / JavaDoesUSB

USB library for Java
MIT License
143 stars 11 forks source link

Can't silence an Exception #25

Closed icemagno closed 4 months ago

icemagno commented 4 months ago

I'm doing this (basicaly your example):

        try {
                for ( var device : Usb.getDevices() ) printDetails(device, "CONNECTED");
                Usb.setOnDeviceConnected(device -> printDetails(device, "CONNECTED"));
                Usb.setOnDeviceDisconnected(device -> printDetails(device, "DISCONNECTED"));
        } catch( Throwable t ) {
            // PLEASE BE QUIET !!
        }

and getting this error not relevant to what I'm doing but very annoying because It's scaring my users:

java.lang.IllegalStateException: Segment size is not a multiple of 2. Size: 7
    at java.base/jdk.internal.foreign.AbstractMemorySegmentImpl.checkArraySize(AbstractMemorySegmentImpl.java:392)
    at java.base/jdk.internal.foreign.AbstractMemorySegmentImpl.toArray(AbstractMemorySegmentImpl.java:352)
    at java.base/jdk.internal.foreign.AbstractMemorySegmentImpl.toArray(AbstractMemorySegmentImpl.java:328)
    at net.codecrete.usb.usbstandard.StringDescriptor.string(StringDescriptor.java:35)
    at net.codecrete.usb.windows.WindowsUsbDeviceRegistry.getStringDescriptor(WindowsUsbDeviceRegistry.java:292)
    at net.codecrete.usb.windows.WindowsUsbDeviceRegistry.lambda$createDevice$2(WindowsUsbDeviceRegistry.java:228)
    at net.codecrete.usb.common.UsbDeviceImpl.setProductString(UsbDeviceImpl.java:215)
    at net.codecrete.usb.windows.WindowsUsbDeviceRegistry.createDevice(WindowsUsbDeviceRegistry.java:228)
    at net.codecrete.usb.windows.WindowsUsbDeviceRegistry.createDeviceFromDeviceInfo(WindowsUsbDeviceRegistry.java:191)
    at net.codecrete.usb.windows.WindowsUsbDeviceRegistry.enumeratePresentDevices(WindowsUsbDeviceRegistry.java:160)
    at net.codecrete.usb.windows.WindowsUsbDeviceRegistry.monitorDevices(WindowsUsbDeviceRegistry.java:123)
    at java.base/java.lang.Thread.run(Thread.java:1570)

I think this is a webcam ( I have one but it was not listed so I think it is the responsible for this ). This error don't broke my executions but is very annoying.

Webcam Multilaser Wc055

Already tried to surround with any Try..Catch possible but it still throwing... What can I do to solve this?

manuelbl commented 4 months ago

Thanks for reporting this. It looks as if the device returns an invalid string descriptor (since the descriptor is in UTF-16, it would need to contain an even number of bytes but doesn't). I will probably change the code to make it slightly more robust.

The exception is thrown and printed in a background thread. So your exception handlers have no effect. The exception is printed using the Java system logger (see https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/lang/System.Logger.html). So if you want to hide it, it's probably best the use a logging library that can consume these log messages and configure it such that it is not printed.

icemagno commented 4 months ago

Thanks. Do you know what class I must suppress in my Logback XML ? Perhaps jdk.internal.foreign.AbstractMemorySegmentImpl.checkArraySize

manuelbl commented 4 months ago

The class to suppress is net.codecrete.usb.windows.WindowsUsbDeviceRegistry.

icemagno commented 4 months ago

Thanks. Closing.