Skip to content

Commit

Permalink
Cleaned up exception handling in TemplateHandler.loadTemplates() to b…
Browse files Browse the repository at this point in the history
…e more specific and less pessimistic.

Removed catch-all "catch (Exception ex)") and replaced with specific exceptions.
Relocated exceptions so that loading will continue with rest of templates if a template fails.
(previously any single failure would abandon loading all other templates as well)

Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
  • Loading branch information
Jules Kerssemakers authored and egonw committed Aug 1, 2010
1 parent 2ef459c commit e1df763
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/main/org/openscience/cdk/layout/TemplateHandler.java
Expand Up @@ -28,6 +28,7 @@
package org.openscience.cdk.layout;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
Expand Down Expand Up @@ -101,6 +102,7 @@ public void loadTemplates(IChemObjectBuilder builder)
line = reader.readLine();
line = "org/openscience/cdk/layout/templates/" + line;
logger.debug("Attempting to read template ", line);
try {
CMLReader structureReader = new CMLReader(
this.getClass().getClassLoader().getResourceAsStream(line)
);
Expand All @@ -109,12 +111,18 @@ public void loadTemplates(IChemObjectBuilder builder)
for (int i = 0; i < files.size(); i++)
templates.add(files.get(i));
logger.debug("Successfully read template ", line);
} catch (CDKException cdke) {
logger.warn("Could not read template ", line, ", reason: ", cdke.getMessage());
logger.debug(cdke);
} catch (IllegalArgumentException iae) {
logger.warn("Could not read template ", line, ", reason: ", iae.getMessage());
logger.debug(iae);
}

}
} catch (Exception exc) {
logger.debug("Could not read templates");
System.out.println("Reason: " + exc.getMessage());
exc.printStackTrace();
logger.debug(exc);
} catch (IOException ioe) {
logger.warn("Could not read (all of the) templates, reason: ", ioe.getMessage());
logger.debug(ioe);
}
}

Expand Down

0 comments on commit e1df763

Please sign in to comment.