nyholku / purejavacomm

Pure Java implementation of JavaComm SerialPort
http://www.sparetimelabs.com/purejavacomm/index.html
BSD 3-Clause "New" or "Revised" License
367 stars 146 forks source link

Why is an IllegalStateException thrown instead of an IOException #133

Open retrodaredevil opened 4 years ago

retrodaredevil commented 4 years ago

I was using this library and for some reason my serial port disconnected, (not the fault of this library). That caused this: https://github.com/nyholku/purejavacomm/blob/d3b6903ec2e220d63e55a06ae6bf6ced929d961e/src/purejavacomm/PureJavaSerialPort.java#L639 line to be ran and an IOException was thrown. My program caught the IOException and tried to continue normally. However, the next time my program tried to write, since the serial port was closed, m_FD was -1, so the write called checkState and this line ran: https://github.com/nyholku/purejavacomm/blob/d3b6903ec2e220d63e55a06ae6bf6ced929d961e/src/purejavacomm/PureJavaSerialPort.java#L1293

In my opinion, throwing an IllegalStateException (PureJavaIllegalStateException) makes no sense, especially since I'm in the middle of a write and an IOException should really be thrown instead. From the javadoc documentation of write: "IOException - if an I/O error occurs. In particular, an IOException is thrown if the output stream is closed"

I think this should be changed to an IOException to be consistent with other OutputStreams. My guess is that there are probably a few other places where an IOException makes more sense than a PureJavaIllegalArgumentException, but I'd like to hear thoughts on that.