0

I'm just getting started with java development. My ex-colleagues write a code(Code 1). I recently faced the timeout exception.

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";
     }

I added a TimeoutException catch block, but eclipse show the error message(Unreachable catch block for TimeoutException. This exception is never thrown from the try statement body).

catch (TimeoutException et) {
             return "Error:" + et.getMessage();
        }

how to handle the timeout exception. Please suggust me.

Jishnu Prathap
  • 1,955
  • 2
  • 21
  • 37
Mahesh M
  • 7
  • 5
  • You need to find out which of those methods throw that exception and then catch it in the appropriate place. But it might be an unchecked exception. What do you plan to do about it, if and when you *do* catch it? – g00se Nov 08 '22 at 12:50
  • Your catch block is inside the if, It should be after the try block has ended. – Jishnu Prathap Nov 08 '22 at 12:52

0 Answers0