From be9d333493a156f8737b7e65bd30d5e4b9942501 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Wed, 14 Sep 2022 09:18:58 +0000 Subject: [PATCH] try to use checked exceptions where APIs already support them git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1904063 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/poi/ooxml/POIXMLDocumentPart.java | 1 + .../poi/ooxml/extractor/POIXMLExtractorFactory.java | 11 ++++++++--- .../java/org/apache/poi/openxml4j/opc/OPCPackage.java | 4 ++-- .../org/apache/poi/xslf/usermodel/XSLFFontInfo.java | 2 +- .../org/apache/poi/xslf/usermodel/XSLFSlideShow.java | 4 ++-- .../org/apache/poi/xssf/usermodel/XSSFHyperlink.java | 1 + .../org/apache/poi/xwpf/usermodel/XWPFDocument.java | 4 ++-- .../org/apache/poi/xwpf/usermodel/XWPFStyles.java | 2 +- 8 files changed, 18 insertions(+), 11 deletions(-) diff --git a/poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLDocumentPart.java b/poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLDocumentPart.java index 3d289ebab7..e99f8ff008 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLDocumentPart.java +++ b/poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLDocumentPart.java @@ -532,6 +532,7 @@ public class POIXMLDocumentPart { * @param descriptor The relationship type to find the part number for * @param minIdx The minimum free index to assign, use -1 for any * @return The next free part number, or -1 if none available + * @throws POIXMLException if the format is invalid */ @Internal public final int getNextPartNumber(POIXMLRelation descriptor, int minIdx) { diff --git a/poi-ooxml/src/main/java/org/apache/poi/ooxml/extractor/POIXMLExtractorFactory.java b/poi-ooxml/src/main/java/org/apache/poi/ooxml/extractor/POIXMLExtractorFactory.java index 93d7016580..8bd8b535bc 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/ooxml/extractor/POIXMLExtractorFactory.java +++ b/poi-ooxml/src/main/java/org/apache/poi/ooxml/extractor/POIXMLExtractorFactory.java @@ -164,11 +164,16 @@ public final class POIXMLExtractorFactory implements ExtractorProvider { return ex; } catch (InvalidFormatException e) { throw new IOException(e); - } catch (RuntimeException | IOException e) { + } catch (IOException e) { if (pkg != null) { pkg.revert(); } throw e; + } catch (RuntimeException e) { + if (pkg != null) { + pkg.revert(); + } + throw new IOException(e); } } @@ -245,7 +250,7 @@ public final class POIXMLExtractorFactory implements ExtractorProvider { } return null; - } catch (Error | RuntimeException | XmlException | OpenXML4JException e) { // NOSONAR + } catch (RuntimeException | XmlException | OpenXML4JException e) { // NOSONAR throw new IOException(e); } // we used to close (revert()) the package here, but this is the callers responsibility @@ -282,7 +287,7 @@ public final class POIXMLExtractorFactory implements ExtractorProvider { fs.close(); } } - } catch (IOException | RuntimeException e) { + } catch (IOException e) { throw e; } catch (Exception e) { throw new IOException(e); diff --git a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/OPCPackage.java b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/OPCPackage.java index d1d8b344f7..3f8d95ac33 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/OPCPackage.java +++ b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/OPCPackage.java @@ -136,8 +136,8 @@ public abstract class OPCPackage implements RelationshipSource, Closeable { /** * Constructor. * - * @param access - * Package access. + * @param access Package access. + * @throws OpenXML4JRuntimeException if there are issues creating properties part */ OPCPackage(PackageAccess access) { if (getClass() != ZipPackage.class) { diff --git a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFFontInfo.java b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFFontInfo.java index 70ce9e522e..2d37f925bc 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFFontInfo.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFFontInfo.java @@ -212,7 +212,7 @@ public class XSLFFontInfo implements FontInfo { try { fntDataIdx = ppt.getPackage().getUnusedPartIndex(fntRel.getDefaultFileName()); } catch (InvalidFormatException e) { - throw new IllegalStateException(e); + throw new IOException(e); } POIXMLDocumentPart.RelationPart rp = ppt.createRelationship(fntRel, XSLFFactory.getInstance(), fntDataIdx, false); diff --git a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFSlideShow.java b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFSlideShow.java index f6bf6b563b..326137b6a1 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFSlideShow.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFSlideShow.java @@ -192,7 +192,7 @@ public class XSLFSlideShow extends POIXMLDocument { try { notes = slidePart.getRelationshipsByType(XSLFRelation.NOTES.getRelation()); } catch(InvalidFormatException e) { - throw new IllegalStateException(e); + throw new IOException(e); } if(notes.isEmpty()) { @@ -200,7 +200,7 @@ public class XSLFSlideShow extends POIXMLDocument { return null; } if(notes.size() > 1) { - throw new IllegalStateException("Expecting 0 or 1 notes for a slide, but found " + notes.size()); + throw new IOException("Expecting 0 or 1 notes for a slide, but found " + notes.size()); } try { diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java index a51ef12b29..05cb2c8d40 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java @@ -58,6 +58,7 @@ public class XSSFHyperlink implements Hyperlink, Duplicatable { * * @param ctHyperlink the xml bean containing xml properties * @param hyperlinkRel the relationship in the underlying OPC package which stores the actual link's address + * @throws IllegalStateException if ctHyperlink Id is null */ protected XSSFHyperlink(CTHyperlink ctHyperlink, PackageRelationship hyperlinkRel) { _ctHyperlink = ctHyperlink; diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java index 77886d52e9..29be42e926 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java @@ -581,10 +581,10 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody { try { parts = getRelatedByType(XWPFRelation.STYLES.getRelation()); } catch (InvalidFormatException e) { - throw new IllegalStateException(e); + throw new IOException(e); } if (parts.length != 1) { - throw new IllegalStateException("Expecting one Styles document part, but found " + parts.length); + throw new IOException("Expecting one Styles document part, but found " + parts.length); } try (InputStream stream = parts[0].getInputStream()) { diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java index 7038315ad3..eb75a3b4d2 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java @@ -91,7 +91,7 @@ public class XWPFStyles extends POIXMLDocumentPart { @Override protected void commit() throws IOException { if (ctStyles == null) { - throw new IllegalStateException("Unable to write out styles that were never read in!"); + throw new IOException("Unable to write out styles that were never read in!"); } XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);