posted 16 years ago
Hi
I am doing a project having a lot of modules. Each module is having its own custom defined exception. Say I have IndexExporter and RequestCreator module having IndexExporterException and RequestCreatorException respectively. These are made to have each module its own specific exception.
The Main module is calling IndexExporter which is calling the RequestCreator. The RequestCreator is having the FileNotFoundException caught using try catch. The exception is wrapped with RequestCreatorEception and thrown. This could be shown in code as:
Request Creator module:
try{
......
}catch(FileNotFoundException e){
throw new RequestCreatorException("File not found, Request Creation Failed", e);
}
This exception is propagated to IndexExporter module where it is again caught and wrapped by IndexExporterException and re-thrown which is then caught by main module.
IndexExporter module:
try{
......
}catch(RequestCreatorException e){
throw new IndexExporterException("Index Exporter Failed", e);
}
Main moudule:
try{
......
}catch(IndexExporterException e){
log.error(e.getMessage(), e);
System.exit(1);
}
If the logging is done in this manner, that produces a complex stack trace. Is this better way to handle the exception?
Should exception be logged as it is caught for the first time as that gives better stacktrace? Then what is the purpose of wrapping the exception?
What is the best way to handle the exception?
Should main catch the exception or it should throw it to JVM?
Amit Arora<br />SCJP 1.4, SCWCD