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
This commit is contained in:
PJ Fanning 2022-09-14 09:18:58 +00:00
parent 7235ff9036
commit be9d333493
8 changed files with 18 additions and 11 deletions

View File

@ -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) {

View File

@ -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);

View File

@ -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) {

View File

@ -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);

View File

@ -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 {

View File

@ -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;

View File

@ -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()) {

View File

@ -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);