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;
//Get customer details
//from local database
return customerDAO

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()

}catch(BaseAppException ex){

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

1 to 1 of 1
1 to 1 of 1