Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Huge exception handling blocks in Java

At the moment i working with library which can throw hell alot of different exceptions(8-10 per method call) and most of them must be handled, worse of all every method (at any time) can throw AuthenticationExpiredException, and i must re-attempt to authenticate. For example:

try {
        xStream = xSet.createXStream(id, binding, mimeType); //Method call
    } catch (AuthenticationExpiredException authenticationExpiredException) {
        try {
        this.authenticate(); // re-authenticate
        xStream = xSet.createXStream(id, binding, mimeType); //Method call again
        } catch (XAMException xamException) {
        throw new ConnectorException(
            "Error occurred during creating new Blob after attempting to re-authenticate",
            xamException);
        }
    } catch (XSystemCorruptException xSystemCorruptException) {
        this.entities.clear();
        this.closeConnection();     

        throw new ConnectorException("XSystem was corrupt and connection was closed",
            xSystemCorruptException);
    } catch (XSetCorruptException xSetCorruptException) {
        this.closeEntity(entity);

        throw new ConnectorException("XSet for entity: " + entity.getXuid()
            + " was currupt and removed", xSetCorruptException);
    } catch (XAMException xamException) {
        throw new ConnectorException(
            "Error occurred during creating new Blob.", xamException);
    }

And this is one of the smallest examples of exception handling. The main question here, is there any way to reduce amount of code which handle exceptions, and make logic cleaner?

UPDATE

Thanks for your feedback. I decided to create separate wrapper for this library by wrapping every method and handling them respectively. To support different handling methods i created interface for wrapper and then implemented it with my custom wrapper like this:

public interface XAMLibraryWrapper{
    // Methods
}

/**
 * Will attempt to recover before throwing RuntimeException
 */
public class RecoveringXAMLibraryWrapper implements XAMLibraryWrapper{
    // Implementation
}
like image 274
YoK Avatar asked Aug 08 '11 13:08

YoK


2 Answers

If there is a consistent way to handle those method (i.e. you always wrap them in the same way and re-throw a RuntimeException, then a custom wrapper library might be the appropriate approach. This can still work when there are 2-3 different ways to handle them (by providing 2-3 wrapper methods (or even classes) for a single wrapped method/class).

Alternatively, if two or more exception types have the exact same handling code, then you can try to look for Java 7 to get multi-catch.

like image 54
Joachim Sauer Avatar answered Sep 19 '22 01:09

Joachim Sauer


You can use Template method pattern. JdbcTemplate is a wonderful example how this design pattern can simplify exception-heavy code (SQLExceptions in this case).

like image 33
Tomasz Nurkiewicz Avatar answered Sep 21 '22 01:09

Tomasz Nurkiewicz