javajschjava-threadsinterrupted-exception

How to know if a thread was interrupted using JSch


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?


Solution

  • When a SftpException or a JSchException is raised to wrap another throwable, its cause will be a Throwable describing the exception that was wrapped.

    Thus, you can check whether .getCause() on either of these returns an InterruptedException.