• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception Handling in n-tier applications

 
Vishu Sharma
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 440
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
Vivek Viswanathan
Ranch Hand
Posts: 350
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thnax a lot karupalli
I really wanted that stuff

Vivek
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic