开发者

Huge exception handling blocks in Java

开发者 https://www.devze.com 2023-03-26 22:09 出处:网络
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 Authent

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 开发者_Python百科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
}


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.


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


If the API is indeed designed to throw that many exceptions, and each of them requires different handling, then there isn't much you can do.

One thing you can do is move repeated code in a separate method, or use Java 7 multi-catch.


If you have different things to do for each exception I'm afraid all those catch statements might be necessary. However, if you have lot's of catch statements with the same content or lot's of resources to close, you might have a look into the new Java 7 features like multiple exceptions in one catch and automatic resource handling. I'm not sure Java 7 is an option for you though.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号