Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Exception handling

  Asked By: Lourdes    Date: Jul 23    Category: Java    Views: 972
  

Can anyone tell me a good approach for exception handling in
java ? Basically i want to reduce the no of exception classes and
centralize the exception handling. For example i want to have two
exception class one extending from RunTimeException to report all the
system exception or unrecoreable exception and another one extending
Exception class to report recoveable cases where user can modify the
data and continue.

If i am using struts for example, instead of each Action class
handling exception, i want to write the BaseAction class which can
take care of exceptin handling

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Devrim Yilmaz     Answered On: Jul 23

U can use it in following structure:


ParentException : which extends Exception, and is also abstract and have an abstract method it can be called translateException().
DataTierException : which extends ParentException, so it supposed to implement translateException method, it's would be implemented like this:
translateException( Exception ex ){
if ( ex instanceof Nullpointer )
throw new DataTierException( SpecificErrorCode )

BusinessTierException : which extends ParentException, so it supposed to implement translateException method, it's would be implemented like this:
translateException( Exception ex ){
if ( ex instanceof Nullpointer )
throw new BusinessTierException ( SpecificErrorCode )
WebTierException : which extends ParentException, so it supposed to implement translateException method, it's would be implemented like this:
translateException( Exception ex ){
if ( ex instanceof Nullpointer )
throw new WebTierException ( SpecificErrorCode )

So in each tier can be like this:

try{
// do sth here
DataTier.methodCall();
}catch( DataTierException ex )
{
throw businessException.translateException( ex );
}

 
Answer #2    Answered By: Ella Brown     Answered On: Jul 23

Struts can be configured for exception  handling via struts-config. It
is called "Declarative Exception Handling" .
Take a look at below url for an overall view of this feature:
javaboutique.internet.com/tutorials/excep_struts/

As you might notice, the goal is to remove exception handling  from
code to configurations classes. You just define the type of exceptions
and the class  that will go to handle them. You can configure it for
each action  too.

I have not used this feature, but seem to be very useful. This applies
to struts  1.1 but maybe new features for this in struts1.2.

 
Didn't find what you were looking for? Find more on Exception handling Or get search suggestion and latest updates.




Tagged: