File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes Exception Handling in n-tier applications Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Exception Handling in n-tier applications" Watch "Exception Handling in n-tier applications" New topic

Exception Handling in n-tier applications

Vishu Sharma

Joined: Feb 07, 2001
Posts: 4
Just a few queries regarding the dos and donts abt Exception handling in an n-tier application. for the purpose lets have a JSP <-> Servlet <-> Session EJB <-> Entity <-> DB kind of scenario.
Here at any tier if i am catching an exception then i can:
1. Do the logging of the exception by first catching and then rethrowing the exception so that it may be processed at the calling level also.
The only disadvantage of this approach is that i am doing exception case processing twice i.e. once at the bean level which is the source of the error and then at the calling levels resp. Exception case processing takes more resources then non-exceptional cases/routine program flow.
2. I log the exception after catching it and then return an status code that wood eventually let the caller know of the exceptional condition that occured in the method call.
The disadvantage is that I have to specifiy a mechanism for sending the status code in case of an exceptional condition and the return type in case of success. The other way round for this approach is that i can use a helper class as like
class Helper implement Serializable // For accross n/w transfer
Object m_ObSuccesReturnValueContainer;
String m_strStatusCode;
Also if this was the recommended approach why does the Java API not use this approach and give the throws mechanism
In the last, approach i wood not catch the exception at anyplace except the top-most level and method wood public returntype functionname(Argument) throws Exception
The last approach is not recommended and the reasoning for which is too obvious. What i am not able to do is decide between the above two approaches.
Again, the use of user-defined exceptions are done only when its a business requirement i.e.
throw new MustMaintainMinimumBalanceException();
Also if someone has a better way of handling exception then pls. make a mention of the same.
Thanx in Advance,
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Please see the nested exception tip in java world. It may solve your problem.
Here is the link.
MindTree Consulting
Matt Midcap

Joined: Jan 07, 1999
Posts: 440
Thank you for your contribution to this forum.
Also, please note - the Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please choose a new name which meets the requirements.

Best Regards,<br />Matt Midcap
Vivek Viswanathan
Ranch Hand

Joined: Mar 03, 2001
Posts: 350
thnax a lot karupalli
I really wanted that stuff


Vivek Viswanathan SCJP 1.2, SCJP 1.6,SCJD,SCEA,SCWCD,IBM-484,IBM-486,IBM-141,Ms.NET C# 70-316,SCMAD, LPIC-I
I agree. Here's the link:
subject: Exception Handling in n-tier applications
It's not a secret anymore!