ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  An Exception Handling Framework for J2EE Applications
Subject:   Bad code example
Date:   2006-01-16 23:40:21
From:   sumitaj
You have provided a good approach for handling exception for a J2EE app . But the code example is not good:

...
CustomerDTO custDTO = null;
try{
//Get customer details
//from local database
customerDAO.getCustomerFromLocalDB();
}catch(CustomerNotActiveException){
...
return customerDAO
.activateCustomerDetails();
}
}


1.Here you are using exceptions for controlling program flow which is a bad programming practice.


2. You add two exceptions to your method signature for handling different error conditions which is a bad approach if the method throws, lets say 5 exceptions.
A better approach would be to add a variable of throwable type to your baseException class and assign the exception to this var.
In the client code you can retrieve this exception and do custom processing using instanceOf .

Like this:



CustomerDAO method:
//throws BaseAppException
public void abc()
throws BaseAppException{
.....
// if not details found
throw new BaseAppException(new CustomerNotActiveException(
"Customer is not active"));
}


Client method:


//catch CustomerNotActiveException


public void xyz()
{
...

try{
...
}catch(BaseAppException ex){
...


if (ex().getCause() instanceof CustomerNotActiveException) {.. }
}


1 to 1 of 1
  1. Bad code example
    2006-01-16 23:58:26  Shrik [View]

1 to 1 of 1