mercredi 11 mars 2015

How to catch FileNotFoundException for new ClassPathXmlApplicationContext() [on hold]

I am trying to create a program where I want to input the spring configurations file from user just to create some demo application. If I directly call new ClassPathXmlApplicationContext(filePath), it throws FileNotFoundException if the file doesn't exist.


I want to catch this exception and display the user an error message instead of terminating the application. Even if I surround the call to constructor by try-catch or using throws declaration, the exception is not caught and the application terminates printing the stack trace.


Any ideas how can I catch the thrown exception?


The caller function -



private String getFlavours(ModelMap model, String configFile) {
FlavourService flavourService = new FlavourService();
String results = null;

try {
results = flavourService.generateFlavour(configFile);
} catch (FileNotFoundException e) {
System.out.println("In FNF Execp");
model.addAttribute("errorMessage", "Requested configurations file not found");
return "index";
}

model.addAttribute("results", results);
model.addAttribute("configFile", configFile);
return "springDemo";
}


The called function -



public String generateFlavour(String configFile) throws FileNotFoundException {
String path = "somepath"; //this is path from classpath
String filename = configFile;

context = new ClassPathXmlApplicationContext(path + filename);

//code to generate required output

return output;
}

Aucun commentaire:

Enregistrer un commentaire