Handling multiple exceptions

Posted by the-banana-king on Stack Overflow See other posts from Stack Overflow or by the-banana-king
Published on 2010-03-15T01:28:47Z Indexed on 2010/03/15 1:29 UTC
Read the original article Hit count: 269

Filed under:
|

Hi there, I have written a class which loads configuration objects of my application and keeps track of them so that I can easily write out changes or reload the whole configuration at once with a single method call. However, each configuration object might potentially throw an exception when doing IO, yet I do not want those errors to cancel the overall process so that the other objects are still given a chance to reload/write. Therefore I collect all exceptions which are thrown while iterating over the objects and store them in a super-exception, which is thrown after the loop, since each exception must still be handled and someone has to be notified of what exactly went wrong. However, that approach looks a bit odd to me. Someone out there with a cleaner solution?

Here is some code of the mentioned class:

public synchronized void store() throws MultipleCauseException
    {
    MultipleCauseException me = new MultipleCauseException("unable to store some resources");
    for(Resource resource : this.resources.values())
        {
        try
            {
            resource.store();
            }
        catch(StoreException e)
            {
            me.addCause(e);
            }
        }
    if(me.hasCauses())
        throw me;
    }

© Stack Overflow or respective owner

Related posts about java

Related posts about exception-handling