【发布时间】:2020-07-28 02:30:16
【问题描述】:
我需要对 JschException 做一些处理。无论异常的具体原因是什么(即 SocketTimeoutException、UnknownHostKey 或 AuthFail),看起来都会引发“JschException”。我确定最初的具体原因的最佳方法是什么?解析 e.getMessage() 字符串是我唯一的选择吗?
这是基本结构。
try {
session = jSch.getSession(getUsername(), host);
session.setUserInfo(user);
session.connect();
} catch (JSchException e) {
e.printStackTrace();
// Here I need to identify the cause and handle accordingly.
}
例如,这里分别是超时和身份验证失败的顶部跟踪。
com.jcraft.jsch.JSchException: Session.connect: java.net.SocketTimeoutException: Read timed out
com.jcraft.jsch.JSchException: Auth fail
如果重要,我想针对身份验证错误启动 try 块的重试,并因超时错误而失败。
【问题讨论】: