Solved

patterns for error handling in Java application...

Posted on 2002-05-06
1
195 Views
Last Modified: 2009-12-16
Hi,

I am working on a web based application which has the following layers. The application conforms to MVC architecture and uses struts
1) JSP, tag handlers
2) Controller (struts)
3) Business tier
4) dB tier (JDO and/or JRF)

We are planning to use apache's Log4j for logging errors to log files. Log4j provides certain methods for logging errors depending upon the error severity. These methods are error(), warn(), debug() and fatal().

However, I intend to control the places where the errors should be logged to the log file. Only one tier should write/log to the log file. The most correct choice seems to be the business tier. The business tier is responsible for translating exceptions from the business tier into application specific exceptions and giving it to the controller layer. It also creates an error message for the user (which will be shown to the user on a error page for example).
The business tier will also log the error/exception to the log file. However, it needs to decide the severity of the error before calling the appropriate Log4j method. In order to do this, it will need some kind of a switch block.

I was wondering if there is a better way of logging errors and propagating exceptions given a multi-tiered application.

Kindly suggest an approach to effective exception and error handling (which involves logging as well).

Thanks
Shayad

0
Comment
Question by:shayad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 4

Accepted Solution

by:
Neutron earned 100 total points
ID: 6993446
Hi Shayrad :-)

One (out of many) possible solutions:

Independent of the tier where an exception is thrown, the implementor knows the best how fatal is the exception that was thrown.

You can have a base class for exceptions in your system, which is constructed not only with a message but with "fatality-level" id.
From this base class you can subclass various families of exceptions, so my suggestion is that system-wide you catch all "foreign" exceptions and convert/throw them as your own exceptions.

In the tier where you want the logging to be perfrmed you can then just "blindly" invoke the getter method for the level-id on the exception which reached that tier.

-

The other way to look at things, you could meddle with proxy-classes (reflection) and decide there what you log and what not. This is the harder way to do it so I won't go into details.

-

A finer version of the prevoius option is to make the smallest use of aspectj (www.aspectj.org) by making pointcuts through some families of classes and perform modular logging of exceptions thrown at locations that don't fit module bounds.

Good luck,
    </ntr> :)
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question