如何处理Smbj中的TimeoutException?捕获块不可达问题求解
处理Smbj代码中的TimeoutException异常问题
刚接触Java开发,同事编写的Smbj代码(如下Code 1)运行时出现TimeoutException,但添加TimeoutException捕获块后,Eclipse提示错误:Unreachable catch block for TimeoutException. This exception is never thrown from the try statement body,求正确处理该超时异常的方法。
Code 1(原代码,存在语法错误)
public String deleteFiles(String DOMAIN0, String USER, String PASSWORD, String SRC_DIR, String DST_DIR, String ext, String ipAddress, String dstRoot) { SmbConfig config = SmbConfig.builder() .withMultiProtocolNegotiate(true) .withSigningRequired(true) .withDfsEnabled(true) .build(); SMBClient client = new SMBClient(config); try { Connection connection = client.connect(ipAddress); AuthenticationContext ac1 = new AuthenticationContext(USER, PASSWORD.toCharArray(), DOMAIN); Session session1 = connection1.authenticate(ac1); DiskShare share1 = (DiskShare) session1.connectShare(SRC_DIR); if (DST_DIR.contains(".")) { if (fileCheckExists) { share1.rm(DST_DIR); else { boolean fileCheckExists = share1.folderExists(DST_DIR); if (fileCheckExists) { share1.rmdir(DST_DIR0, true); catch (TimeoutException et) { return "Error:" + et.getMessage(); catch (Exception ex) { return "Error:" + ex.getMessage(); return "Completed"; }
添加的捕获块代码
catch (TimeoutException et) { return "Error:" + et.getMessage(); }
解决步骤
1. 先修复原代码的语法错误
原代码存在变量名错误、大括号未闭合、变量未定义等语法问题,这些会导致编译器误判异常捕获逻辑,修复后的代码如下:
public String deleteFiles(String DOMAIN0, String USER, String PASSWORD, String SRC_DIR, String DST_DIR, String ext, String ipAddress, String dstRoot) { SmbConfig config = SmbConfig.builder() .withMultiProtocolNegotiate(true) .withSigningRequired(true) .withDfsEnabled(true) .build(); SMBClient client = new SMBClient(config); try { Connection connection = client.connect(ipAddress); AuthenticationContext ac1 = new AuthenticationContext(USER, PASSWORD.toCharArray(), DOMAIN0); Session session1 = connection.authenticate(ac1); DiskShare share1 = (DiskShare) session1.connectShare(SRC_DIR); if (DST_DIR.contains(".")) { boolean fileCheckExists = share1.fileExists(DST_DIR); if (fileCheckExists) { share1.rm(DST_DIR); } } else { boolean fileCheckExists = share1.folderExists(DST_DIR); if (fileCheckExists) { share1.rmdir(DST_DIR, true); } } } catch (TimeoutException et) { return "Error:" + et.getMessage(); } catch (Exception ex) { return "Error:" + ex.getMessage(); } return "Completed"; }
2. 针对超时异常的正确处理
编译器提示“不可达捕获块”的核心原因是:try块中的方法并未声明抛出标准的java.util.concurrent.TimeoutException(checked异常),此时超时异常要么被包装在Smbj的专属异常中,要么是RuntimeException子类。
方式一:捕获SMBApiException并判断超时错误码
Smbj的大部分操作异常会封装在SMBApiException中,超时对应错误码STATUS_IO_TIMEOUT,可以针对性处理:
catch (SMBApiException e) { if (e.getStatusCode() == StatusCode.STATUS_IO_TIMEOUT) { return "Error: 操作超时 - " + e.getMessage(); } return "Error: SMB操作失败 - " + e.getMessage(); } catch (Exception ex) { return "Error:" + ex.getMessage(); }
方式二:捕获RuntimeException类型的TimeoutException
如果Smbj抛出的是继承自RuntimeException的自定义TimeoutException,直接将捕获块放在Exception前面即可(此时编译器不会提示不可达):
catch (TimeoutException et) { return "Error:" + et.getMessage(); } catch (Exception ex) { return "Error:" + ex.getMessage(); }
方式三:配置Smbj的超时参数
在SmbConfig中明确设置超时时间,从根源避免无限制等待:
SmbConfig config = SmbConfig.builder() .withMultiProtocolNegotiate(true) .withSigningRequired(true) .withDfsEnabled(true) .withTimeout(10, TimeUnit.SECONDS) // 设置10秒超时 .build();
内容的提问来源于stack exchange,提问作者Mahesh M
相关产品推荐
相关产品推荐

