David Krell
David Krell

Reputation: 228

How can I urge the application to continue after an exception is thrown?

I am working on an application which shall move some files and update some inputs in the database. If the file isn't found there will be thrown my own Exception. But after that exception the application interrupts and stops moving files. How can I change my code so that my application can continue after the exception?

It shall just print out that the file isn't existing and then it shall continue with moving the next file.

Here is my code:

import java.sql.*;      
import java.nio.file.*;
import java.io.*;

public class SortMainNewFinal {
  public static void main(String[] args) throws IOException, FileNotSynchronizedException {
   
  //some not relevant code
  //...

  //the relevant code

    if(path.contains(timestamp)) { 
      if(filecheck(path,filename)) { 
      data.writeToFile("The File " + filename + " is already in its subordinated folder.");
      }
    }else {
       checkDir(path+timestamp,filename); 
       filemove(path,timestamp,filename); 
       data.writeToFile("File "+filename+" has been moved into " + path + timestamp + "/" + 
                        filename);
                    
       String strUpdate = ("update cm_documents set document_path=\""+path+timestamp+"/"
                            +filename + "\" where document_name=\""+filename+"\"");
       data.writeToFile("SQL Statement is: " + strUpdate); 
       update.executeUpdate(strUpdate); 
    }
      //Catch SQLException
  }

  private static void filemove(String path, String timestamp, String filename) 
  throws IOException, FileNotSynchronizedException{
        try {
          Files.move(Paths.get(path+filename), Paths.get(path+timestamp+"/"+filename));
        }catch(NoSuchFileException e) {
          String error= "ERROR: The file " + filename + " has been moved, renamed or deleted and 
                         these changes are not synchronized with the database.";
            
          String file_name="C:/database/logfile.log";
          WriteFile data = new WriteFile(file_name,true);
            
          data.writeToFile(error); 
          throw new FileNotSynchronizedException(filename); 
        }
 }
} 
class FileNotSynchronizedException extends Exception{

  FileNotSynchronizedException(String filename) throws IOException{
    super("The file" + filename + "has been moved, renamed or deleted and these changes are not 
           synchronized with the database.");
    
 }
}

My problem is now that the application throws my exception and then interrupts but I want the application to print out the Exception and then it shall just continue at the top of my application with the if Statement etc.

I want to say that I am not a mother tongue of English so please excuse any mistakes and I am quite new to coding so please excuse any mistakes in my code.

Upvotes: 2

Views: 128

Answers (1)

Fidan Bicaj
Fidan Bicaj

Reputation: 94

Remove the throws IOException, FileNotSynchronizedException from your main method, because there you want to handle the exception not throw it. If you leave you're catch block empty it will not stop the program execution:

    try {
        filemove(path,timestamp,filename);
    }catch ( Exception e){
        System.out.println("Some exception occurred ->  "+e.getMessage());
    }

Upvotes: 1

Related Questions