An Exception Handling Framework for J2EE Applications
Pages: 1, 2, 3, 4, 5, 6
The data coming from exceptioninfo.xml for each exception can be encapsulated into a data transfer object (DTO) named ExceptionInfoDTO. Now we also need a placeholder where we could cache these objects, as we wouldn't want to parse the XML file again and again and create objects each time an exception occurs. This work can be delegated to a class named ExceptionInfoCache, which will cache all ExceptionInfoDTO objects after reading their information from exceptioninfo.xml.
What's this fuss all about, huh? The core of all this is the ExceptionHandler implementation, which will use data encapsulated in ExceptionInfoDTO for getting the message code, creating ExceptionDTO objects, and then logging it based on the type of logging specified in ExceptionInfoDTO for a given exception.
Here is the handleException method of an ExceptionHandler implementation.
public ExceptionDTO handleException(String userId,
BaseAppException exp) {
ExceptionDTO exDTO = new ExceptionDTO();
ExceptionInfoCache ecache =
ExceptionInfoCache.getInstance();
ExceptionInfo exInfo = ecache
.getExceptionInfo(
ExceptionHelper.getClassName(exp));
String loggingType = null;
if (exInfo != null) {
loggingType = exInfo.getLoggingType();
exDTO.setConfirmation(exInfo
.isConfirmation());
exDTO.setMessageCode(exInfo
.getMessageCode());
}
FileLogger logger = new FileLoggerFactory()
.create();
logger.logException(exp, loggingType);
Depending upon business requirements, there can be multiple implementations of the ExceptionHandler interface. Deciding which implementation to use can be delegated to a Factory, specifically a ExceptionHandlerFactory class.
Conclusion
Without a comprehensive exception-handing strategy, an ad hoc collection of exception-handling blocks can lead to non-standard error handling and non-maintainable code. Using the above approach, exception handling can be streamlined in a J2EE application.
Resources
- Sample code for this article
- Design Patterns: Source of the Facade and Template Method patterns
ShriKant Vashishtha currently works as a solution architect for Tata Consultancy Services Limited (TCS), India.
Return to ONJava.com.
-
Problem in integrating the code.
2008-05-27 07:36:41 rohit566 [View]
-
nice article
2007-08-07 21:36:57 daringtakers [View]
-
Request Processor
2007-02-23 12:40:45 scranthdaddy [View]
-
logging original stack trace
2007-02-07 12:35:23 scranthdaddy [View]
-
logging original stack trace
2007-02-07 20:55:33 Shrik [View]
-
logging original stack trace
2007-02-07 20:53:14 Shrik [View]
-
making aop based
2006-10-08 19:05:03 aoplearner [View]
-
making aop based
2006-10-08 19:04:59 aoplearner [View]
-
Exception handling framework
2006-04-13 06:38:56 angryguy [View]
-
Exception handling framework
2006-04-13 08:52:02 Shrik [View]
-
useful ideas
2006-02-12 13:52:08 dario_ [View]
-
useful ideas
2006-02-12 18:29:58 Shrik [View]
-
Bad code example
2006-01-16 23:40:21 sumitaj [View]
-
Bad code example
2006-01-16 23:58:26 Shrik [View]
-
?
2006-01-13 06:04:32 namakemono [View]
-
?
2006-01-13 09:13:37 Shrik [View]
-
Missing Important New Approaches
2006-01-12 23:51:16 thomasvan de velde [View]
-
thsi approach not fit in SOA Based application
2007-10-09 02:58:14 munira.shaikh [View]
-
thsi approach not fit in SOA Based application
2007-10-09 02:58:11 munira.shaikh [View]
-
thsi approach not fit in SOA Based application
2007-10-09 02:57:55 munira.shaikh [View]
-
Missing Important New Approaches
2006-01-18 06:01:59 Shrik [View]
-
ignorant
2006-01-12 11:04:53 dbwduh [View]