Solved

patterns for error handling in Java application...

Posted on 2002-05-06
1
189 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
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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 …

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now