Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

patterns for error handling in Java application...

Posted on 2002-05-06
1
Medium Priority
?
197 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 400 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

721 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