diff --git a/src/main/java/jssc/SerialNativeInterface.java b/src/main/java/jssc/SerialNativeInterface.java index 5da95e399..40022472e 100644 --- a/src/main/java/jssc/SerialNativeInterface.java +++ b/src/main/java/jssc/SerialNativeInterface.java @@ -277,8 +277,7 @@ public static String getLibraryVersion() { * * @return If the operation is successfully completed, the method returns true, otherwise false */ - public native boolean writeBytes(long handle, byte[] buffer) - throws java.io.IOException; + public native boolean writeBytes(long handle, byte[] buffer) throws IOException; /** * Get bytes count in buffers of port diff --git a/src/main/java/jssc/SerialPort.java b/src/main/java/jssc/SerialPort.java index 588f9bb05..713fa769c 100644 --- a/src/main/java/jssc/SerialPort.java +++ b/src/main/java/jssc/SerialPort.java @@ -24,6 +24,7 @@ */ package jssc; +import java.io.IOException; import java.io.UnsupportedEncodingException; import java.lang.reflect.Method; @@ -406,9 +407,13 @@ public boolean setDTR(boolean enabled) throws SerialPortException { * * @throws SerialPortException if exception occurred */ - public boolean writeBytes(byte[] buffer) throws SerialPortException, java.io.IOException { + public boolean writeBytes(byte[] buffer) throws SerialPortException { checkPortOpened("writeBytes()"); - return serialInterface.writeBytes(portHandle, buffer); + try { + return serialInterface.writeBytes(portHandle, buffer); + } catch(IOException ex) { + throw SerialPortException.wrapNativeException(ex, this, "writeBytes"); + } } /** @@ -422,7 +427,7 @@ public boolean writeBytes(byte[] buffer) throws SerialPortException, java.io.IOE * * @since 0.8 */ - public boolean writeByte(byte singleByte) throws SerialPortException, java.io.IOException { + public boolean writeByte(byte singleByte) throws SerialPortException { checkPortOpened("writeByte()"); return writeBytes(new byte[]{singleByte}); } @@ -438,7 +443,7 @@ public boolean writeByte(byte singleByte) throws SerialPortException, java.io.IO * * @since 0.8 */ - public boolean writeString(String string) throws SerialPortException, java.io.IOException { + public boolean writeString(String string) throws SerialPortException { checkPortOpened("writeString()"); return writeBytes(string.getBytes()); } @@ -455,7 +460,7 @@ public boolean writeString(String string) throws SerialPortException, java.io.IO * * @since 2.8.0 */ - public boolean writeString(String string, String charsetName) throws SerialPortException, UnsupportedEncodingException, java.io.IOException { + public boolean writeString(String string, String charsetName) throws SerialPortException, UnsupportedEncodingException { checkPortOpened("writeString()"); return writeBytes(string.getBytes(charsetName)); } @@ -471,7 +476,7 @@ public boolean writeString(String string, String charsetName) throws SerialPortE * * @since 0.8 */ - public boolean writeInt(int singleInt) throws SerialPortException, java.io.IOException { + public boolean writeInt(int singleInt) throws SerialPortException { checkPortOpened("writeInt()"); return writeBytes(new byte[]{(byte)singleInt}); } @@ -487,7 +492,7 @@ public boolean writeInt(int singleInt) throws SerialPortException, java.io.IOExc * * @since 0.8 */ - public boolean writeIntArray(int[] buffer) throws SerialPortException, java.io.IOException { + public boolean writeIntArray(int[] buffer) throws SerialPortException { checkPortOpened("writeIntArray()"); byte[] byteArray = new byte[buffer.length]; for(int i = 0; i < buffer.length; i++){ diff --git a/src/main/java/jssc/SerialPortException.java b/src/main/java/jssc/SerialPortException.java index ba35e7684..b075aef40 100644 --- a/src/main/java/jssc/SerialPortException.java +++ b/src/main/java/jssc/SerialPortException.java @@ -67,6 +67,9 @@ public class SerialPortException extends Exception { */ final public static String TYPE_INCORRECT_SERIAL_PORT = "Incorrect serial port"; + /** Exception occurred in native code */ + final public static String TYPE_NATIVE_EXCEPTION = "Native exception occurred: %s"; + /** Serial port object **/ private SerialPort port; /** Method name **/ @@ -110,6 +113,10 @@ public SerialPortException(String portName, String methodName, String exceptionT this.exceptionType = exceptionType; } + public static SerialPortException wrapNativeException(Exception ex, SerialPort port, String methodName) { + return new SerialPortException(port, methodName, String.format(TYPE_NATIVE_EXCEPTION, ex.getLocalizedMessage())); + } + /** * Getting port name during operation with which the exception was called * Deprecated: Use getPort().getName() instead.