I am using com.jcraft.jsch library to make remote SFTP sessions. It is a convenient library and easy to use.
While I used the library in a single-threaded scenario no problems arose, but now I am using it inside one of many threads in my application.
The problem I found is Jsch seems to trap InterruptedException before my own code, just to rethrow it as a different exception, SftpException or JschException.
If I pretend that any SftpException and JschException came from an InterruptedException then I can gently stop the thread and everything flows fine. However, this is dangerous because I am masking real SftpException/JschException exceptions due to Internet transmission errors, wrong credentials, etc.
How can I be sure that an InterruptedException occurred inside Jsch?
When a
SftpExceptionor aJSchExceptionis raised to wrap another throwable, itscausewill be aThrowabledescribing the exception that was wrapped.Thus, you can check whether
.getCause()on either of these returns anInterruptedException.