RSS

#3 Exception Catch Blocks

01 Oct

Exception handling must be done correctly so that the problems are accurately reported, and so that problems can be addressed quickly and effectively. Poorly coded exception handling causes a risk that will not be noticed until there is a critical customer problem.  Then it is too late. Common kinds of mistakes include:

  • Failing to describe the function itself that is failing
  • Attempting to guess what exception it just caught, without grounds for such assumptions, and making a new exception that duplicates that (possibly inaccurate) guess.
  • Failing to include parameters that include critical details about the situation that failed
  • Failing to catch all exception, and only handling a specific subset
  • Handling different kinds of caught exceptions arbitrarily differently: wrapping some and not others
  • Failing to rethrow based on an assumption that the exception must be innocuous.

I have encountered numerous example where the exception message was simply wrong: it did not describe the problem. Inappropriately, it described a different situation, which wastes time. Sloppy coding will waste not only the time of customers, but also consultants and support team.

Example of Poor Exception Handling

public Object createInstance(String className) throws FrameWorkException {
    final String CLASSFUNCTION = CLASSNAME + ".createInstance(): ";
    logger.debug(CLASSFUNCTION + "Entered ");
    logger.debug(CLASSFUNCTION + "received class name : " + className);
    Object instanceObject = m_objectMap.get(className);
    if (instanceObject == null) {
        try {
            className = className.trim();
            Class loadedClass = this.getClass().getClassLoader().loadClass(
                    className);
            instanceObject = loadedClass.newInstance();
            m_objectMap.put(className, instanceObject);
        } catch (InstantiationException instExcep) {
            throw new FrameWorkException(
                    "InstantiationException for class :" + className,
                    instExcep);
        } catch (ClassNotFoundException clsNotFoundExcep) {
            throw new FrameWorkException(
                    "ClassNotFoundException for class :" + className,
                    clsNotFoundExcep);
        } catch (IllegalAccessException iae) {
            throw new FrameWorkException(
                    "IllegalAccessException for class :" + className, iae);
        }
    }
    logger.debug(CLASSFUNCTION + "Exit ");
    return instanceObject;
}

Improved Exception Handling

public Object createInstance(String className) throws FrameWorkException {
    final String CLASSFUNCTION = CLASSNAME + ".createInstance(): ";
    logger.debug(CLASSFUNCTION + "Entered ");
    logger.debug(CLASSFUNCTION + "received class name : " + className);
    Object instanceObject = m_objectMap.get(className);
    if (instanceObject == null) {
        try {
            className = className.trim();
            Class loadedClass = this.getClass().getClassLoader().loadClass(
                    className);
            instanceObject = loadedClass.newInstance();
            m_objectMap.put(className, instanceObject);
        } catch (Exception eee) {
            throw new FrameWorkException(
                    ErrorMessage.FAILED_TO_CREATE_INSTANCE_OF_CLASS, eee,
                    {className}  );
        }
    logger.debug(CLASSFUNCTION + "Exit ");
    return instanceObject;
}

Consider this booklet on exception handling:  Articulate Error Handling

Advertisements
 
2 Comments

Posted by on October 1, 2011 in Coding

 

Tags:

2 responses to “#3 Exception Catch Blocks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: