diff --git a/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/driver/CommonUsbSerialPort.java b/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/driver/CommonUsbSerialPort.java index 7762e5ac..f805433b 100644 --- a/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/driver/CommonUsbSerialPort.java +++ b/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/driver/CommonUsbSerialPort.java @@ -117,6 +117,7 @@ public void open(UsbDeviceConnection connection) throws IOException { throw new IllegalArgumentException("Connection is null"); } mConnection = connection; + boolean ok = false; try { openInt(); if (mReadEndpoint == null || mWriteEndpoint == null) { @@ -124,11 +125,13 @@ public void open(UsbDeviceConnection connection) throws IOException { } mUsbRequest = new UsbRequest(); mUsbRequest.initialize(mConnection, mReadEndpoint); - } catch(Exception e) { - try { - close(); - } catch(Exception ignored) {} - throw e; + ok = true; + } finally { + if (!ok) { + try { + close(); + } catch (Exception ignored) {} + } } }