File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

Exception Handling in n-tier applications

Vishu Sharma
Greenhorn

Joined: Feb 07, 2001
Posts: 4
Hi,
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.
if(balance<1000)
throw new MustMaintainMinimumBalanceException();
Also if someone has a better way of handling exception then pls. make a mention of the same.
Thanx in Advance,
Vishal
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,
Please see the nested exception tip in java world. It may solve your problem.
Here is the link. http://www.javaworld.com/javaworld/javatips/jw-javatip91.html
Rgds
Kiran
MindTree Consulting
Matt Midcap
Sheriff

Joined: Jan 07, 1999
Posts: 440
"karupalli"
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 http://www.javaranch.com/name.jsp. 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.
Thanks.


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


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
 
Consider Paul's rocket mass heater.
 
subject: Exception Handling in n-tier applications
 
Similar Threads
Which way to go - A design question.
Exception Handling in n-tier applications
Reg. Exception hierarchy
Handling JSP exceptions in Struts2‏
Detecting that a String represents a valid number