|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 6 of 7
public class DatabaseFailureExceptionHandler extends CriticalExceptionHandler{
public void handleException(DatabaseFailureException e)
throws ExampleException
{
// send mail to the database support team
sendMail("dba", "error", "fix the database please "+ e.getMessage() );
}
}
Should the application encounter a database failure, the database failure exception handler will be instantiated, as I'll explain. The simple handler developed for this article will send email to the database support personnel to ensure that they are monitoring the application database.
Now that I've developed the simple error-handling framework, I'll explain how the exception handlers are instantiated and
invoked. As I've mentioned earlier, the exception service will retrieve the class name of the specific exception that has
been thrown using the getName() method of the Class object.
This method will return a string with the fully qualified name. The ExceptionService will use this name to dynamically load the exception handler associated with this exception, as follows:
try {
ExceptionHandler exceptionHandler = loadExceptionHandler( exception);
...
} catch ( ClassNotFoundException cnfe){
// load default exception handler
...
}
The ExceptionHandler is loaded dynamically, based on the objects' naming convention. It's true that this scheme limits your naming choices; however,
the resulting design is greatly simplified. The code in the try-catch block shown above uses a private method in the exception
service to load the corresponding exception-handling code. This method can be developed as shown below:
private ExceptionHandler loadExceptionHandler( String sExceptionName, Exception exception)
{
String HANDLER = "Handler"; // Should come from a properties file
String sClassName = null;
if (sExceptionName == null && exception == null) {
// load the default exception handler and return
...
}
if ( sExceptionName == null) {
// the sExceptionName is passed in from the parent class
Class classException = exception.getClass();
sClassName = classException.getName();
} else {
sClassName = sExceptionName;
}
// The string to load is the name of the exception plus the string
// identifying it as a handler
String sExceptionHandler = classException.getName()+ HANDLER;
ExceptionHandler ehExceptionHandler = null;
try{
// attempt to load the exception handler using the class.forName
} catch (ClassNotFoundException exClassNotFound){
// Get the name of the parent class of the exception
sExceptionHandler = classException.getSuperClass().getName();
if ( sExceptionHandler != DEFAULTEXCEPTIONHANDLER ){
// load the parent class exception handler
// (this is a recursive method,
// until the DefaultExceptionHandler is returned)
loadExceptionHandler( sExceptionHandler, exception);
}else{
// load the default error handler
}
}
return ehExceptionHandler;
}
This exception-handling framework lets you build up your exception handling to the extent necessary to achieve your required
functionality. The exception handler will work its way up the exception handler hierarchy and load the most specific exception
handler. Should it traverse the entire tree up to the DefaultExceptionHandler, it will load that handler. What have you gained through this framework? You now have the confidence that an exception handler
will handle the exception. You also have the ability to handle specific errors, such as database failures, differently than
presentation failures, through custom ExceptionHandlers. The processing required to load the different exception handling routines is handled through centralized code, thereby reducing
the project's coding effort.