fix up use of ExceptionUtil in main poi module

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1907651 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2023-02-14 14:26:56 +00:00
parent 03f7f911b0
commit 3a70d5e002
34 changed files with 224 additions and 2 deletions

View File

@ -45,6 +45,7 @@ import org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIEncryptor;
import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.DocumentInputStream;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.ExceptionUtil;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Internal;
@ -245,6 +246,9 @@ public abstract class POIDocument implements Closeable {
} catch (IOException e) {
throw e;
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
throw new IOException("Error "+step+" property set with name " + setName, e);
} finally {
IOUtils.closeQuietly(encPoifs);

View File

@ -35,6 +35,7 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.hpsf.wellknown.PropertyIDMap;
import org.apache.poi.util.CodePageUtil;
import org.apache.poi.util.ExceptionUtil;
import org.apache.poi.util.HexDump;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.LittleEndianByteArrayInputStream;
@ -403,6 +404,9 @@ public class Property {
try {
write(bos, codepage);
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
LOG.atWarn().withThrowable(e).log("can't serialize string");
}
@ -475,6 +479,9 @@ public class Property {
return LocaleUtil.getLocaleFromLCID(((Number)value).intValue());
}
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
LOG.atWarn().log("Can't decode id {}", box(getID()));
}
return null;

View File

@ -35,6 +35,7 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.hpsf.wellknown.PropertyIDMap;
import org.apache.poi.util.CodePageUtil;
import org.apache.poi.util.ExceptionUtil;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.LittleEndianByteArrayInputStream;
@ -547,6 +548,9 @@ public class Section {
} catch (HPSFRuntimeException ex) {
throw ex;
} catch (Exception ex) {
if (ExceptionUtil.isFatal(ex)) {
ExceptionUtil.rethrow(ex);
}
throw new HPSFRuntimeException(ex);
}
}

View File

@ -25,6 +25,7 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.crypt.ChunkedCipherInputStream;
import org.apache.poi.poifs.crypt.Decryptor;
import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.util.ExceptionUtil;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Internal;
import org.apache.poi.util.LittleEndian;
@ -61,6 +62,9 @@ public final class Biff8DecryptingStream implements BiffHeaderInput, LittleEndia
ccis.readFully(initialBuf);
}
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
throw new RecordFormatException(e);
}
}

View File

@ -105,6 +105,7 @@ import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.ss.util.WorkbookUtil;
import org.apache.poi.util.Configurator;
import org.apache.poi.util.ExceptionUtil;
import org.apache.poi.util.HexDump;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Internal;
@ -1584,6 +1585,9 @@ public final class HSSFWorkbook extends POIDocument implements Workbook {
}
os.close();
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
throw new EncryptedDocumentException(e);
}
}

View File

@ -34,6 +34,7 @@ import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.RC2ParameterSpec;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.util.ExceptionUtil;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Internal;
import org.apache.poi.util.LittleEndian;
@ -346,6 +347,9 @@ public final class CryptoFunctions {
Class<Provider> clazz = (Class<Provider>)cl.loadClass(bcProviderName);
Security.addProvider(clazz.getDeclaredConstructor().newInstance());
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
throw new EncryptedDocumentException("Only the BouncyCastle provider supports your encryption settings - please add it to the classpath.", e);
}
}

View File

@ -35,6 +35,7 @@ import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.BitField;
import org.apache.poi.util.BitFieldFactory;
import org.apache.poi.util.ExceptionUtil;
import org.apache.poi.util.LittleEndianInput;
/**
@ -141,6 +142,9 @@ public class EncryptionInfo implements GenericRecord {
try {
eib = getBuilder(encryptionMode);
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
throw new IOException(e);
}
@ -187,6 +191,9 @@ public class EncryptionInfo implements GenericRecord {
try {
eib = getBuilder(encryptionMode);
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
throw new EncryptedDocumentException(e);
}

View File

@ -24,6 +24,7 @@ import org.apache.poi.poifs.crypt.ChainingMode;
import org.apache.poi.poifs.crypt.CipherAlgorithm;
import org.apache.poi.poifs.crypt.EncryptionHeader;
import org.apache.poi.poifs.crypt.HashAlgorithm;
import org.apache.poi.util.ExceptionUtil;
import org.apache.poi.util.GenericRecordUtil;
public class AgileEncryptionHeader extends EncryptionHeader {
@ -48,6 +49,9 @@ public class AgileEncryptionHeader extends EncryptionHeader {
throw new NullPointerException("keyData not set");
}
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
throw new EncryptedDocumentException("Unable to parse keyData");
}

View File

@ -42,6 +42,7 @@ import org.apache.poi.poifs.filesystem.DocumentInputStream;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.BitField;
import org.apache.poi.util.BitFieldFactory;
import org.apache.poi.util.ExceptionUtil;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.LittleEndianInputStream;
@ -212,6 +213,8 @@ public class CryptoAPIDecryptor extends Decryptor {
throw (GeneralSecurityException)e;
} else if (e instanceof IOException) {
throw (IOException)e;
} else if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
} else {
throw new IOException("summary entries can't be read", e);
}

View File

@ -24,6 +24,7 @@ import javax.crypto.Cipher;
import org.apache.commons.io.input.BoundedInputStream;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.util.ExceptionUtil;
import org.apache.poi.util.Internal;
/**
@ -59,6 +60,9 @@ import org.apache.poi.util.Internal;
cipher.update(oneByte, 0, 1, oneByte, 0);
super.write(oneByte);
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
throw new EncryptedDocumentException(e);
}
}
@ -69,6 +73,9 @@ import org.apache.poi.util.Internal;
cipher.update(b, off, len, b, off);
super.write(b, off, len);
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
throw new EncryptedDocumentException(e);
}
}

View File

@ -180,14 +180,17 @@ public final class CleanerUtil {
(PrivilegedAction<Throwable>) () -> {
try {
unmapper.invokeExact(buffer);
return null;
} catch (Throwable t) {
if (ExceptionUtil.isFatal(t)) {
ExceptionUtil.rethrow(t);
}
}
return null;
});
if (error != null) {
if (ExceptionUtil.isFatal(error)) {
ExceptionUtil.rethrow(error);
}
throw new IOException("Unable to unmap the mapped buffer", error);
}
};

View File

@ -35,6 +35,7 @@ import org.apache.poi.poifs.filesystem.FileMagic;
import org.apache.poi.sl.usermodel.PictureData;
import org.apache.poi.sl.usermodel.PictureShape;
import org.apache.poi.sl.usermodel.RectAlign;
import org.apache.poi.util.ExceptionUtil;
public class DrawPictureShape extends DrawSimpleShape {
@ -103,7 +104,10 @@ public class DrawPictureShape extends DrawSimpleShape {
}
} catch (NoSuchElementException ignored) {
break;
} catch (Exception | ServiceConfigurationError ignored) {
} catch (Exception | ServiceConfigurationError e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
}
}

View File

@ -47,6 +47,7 @@ import org.apache.poi.ss.usermodel.ShapeContainer;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.Beta;
import org.apache.poi.util.ExceptionUtil;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.LocaleUtil;
@ -132,6 +133,9 @@ public class EmbeddedExtractor implements Iterable<EmbeddedExtractor> {
data = new EmbeddedData(od.getFileName(), od.getObjectData(), od.getContentType());
}
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
LOG.atWarn().withThrowable(e).log("Entry not found / readable - ignoring OLE embedding");
}
} else if (shape instanceof Picture) {

View File

@ -51,6 +51,7 @@ import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.ExceptionUtil;
import org.apache.poi.util.LocaleUtil;
import org.apache.poi.util.StringUtil;
@ -509,6 +510,9 @@ public class EvaluationConditionalFormatRule implements Comparable<EvaluationCon
// see TextFunction.TRIM for implementation
return StringUtil.isBlank(v);
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
// not a valid string value, and not a blank cell (that's checked earlier)
return false;
}
@ -518,6 +522,9 @@ public class EvaluationConditionalFormatRule implements Comparable<EvaluationCon
// see TextFunction.TRIM for implementation
return StringUtil.isNotBlank(v);
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
// not a valid string value, but not blank
return true;
}

View File

@ -42,6 +42,7 @@ import org.apache.poi.ss.formula.ptg.Ref3DPtg;
import org.apache.poi.ss.formula.ptg.Ref3DPxg;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.ss.util.CellReference.NameType;
import org.apache.poi.util.ExceptionUtil;
import org.apache.poi.util.LocaleUtil;
/**
@ -257,6 +258,9 @@ public final class OperationEvaluationContext {
}
return new LazyAreaEval(0, absoluteC, ssVersion.getLastRowIndex(), absoluteC, sre);
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
return ErrorEval.REF_INVALID;
}
}
@ -279,6 +283,9 @@ public final class OperationEvaluationContext {
}
return new LazyAreaEval(absoluteR, 0, absoluteR, ssVersion.getLastColumnIndex(), sre);
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
return ErrorEval.REF_INVALID;
}
}

View File

@ -23,6 +23,7 @@ import java.util.regex.Pattern;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.util.ExceptionUtil;
/**
* Formats sheet names for use in formula expressions.
@ -68,6 +69,9 @@ public final class SheetNameFormatter {
appendAndEscape(out, rawSheetName);
}
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
throw new IllegalStateException(e);
}
}
@ -96,6 +100,9 @@ public final class SheetNameFormatter {
appendOrREF(out, rawSheetName);
}
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
throw new IllegalStateException(e);
}
}
@ -125,6 +132,9 @@ public final class SheetNameFormatter {
sb.append(ch);
}
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
throw new IllegalStateException(e);
}
}

View File

@ -40,6 +40,7 @@ import org.apache.poi.ss.formula.udf.UDFFinder;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.util.CellRangeAddressBase;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.ExceptionUtil;
import org.apache.poi.util.Internal;
import static org.apache.logging.log4j.util.Unbox.box;
@ -332,6 +333,9 @@ public final class WorkbookEvaluator {
String msg = "Error evaluating cell " + cr.formatAsString();
return new NotImplementedException(msg, inner);
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
// avoid bombing out during exception handling
LOG.atError().withThrowable(e).log("Can't add exception info");
return inner; // preserve original exception

View File

@ -24,6 +24,7 @@ import org.apache.poi.ss.formula.eval.EvaluationException;
import org.apache.poi.ss.formula.eval.OperandResolver;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.formula.functions.FreeRefFunction;
import org.apache.poi.util.ExceptionUtil;
import static org.apache.poi.ss.formula.eval.RelationalOperationEval.EqualEval;
@ -53,6 +54,9 @@ public final class Switch implements FreeRefFunction {
try {
expression = OperandResolver.getSingleValue(args[0], ec.getRowIndex(), ec.getColumnIndex());
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
return ErrorEval.NA;
}

View File

@ -23,6 +23,7 @@ import org.apache.poi.ss.formula.eval.*;
import org.apache.poi.ss.formula.functions.ArrayFunction;
import org.apache.poi.ss.formula.functions.FreeRefFunction;
import org.apache.poi.ss.formula.functions.LookupUtils;
import org.apache.poi.util.ExceptionUtil;
/**
* Implementation of Excel function XLOOKUP()
@ -81,6 +82,9 @@ final class XLookupFunction implements FreeRefFunction, ArrayFunction {
} catch (EvaluationException e) {
return e.getErrorEval();
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
return ErrorEval.VALUE_INVALID;
}
}
@ -93,6 +97,9 @@ final class XLookupFunction implements FreeRefFunction, ArrayFunction {
} catch (EvaluationException e) {
return e.getErrorEval();
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
return ErrorEval.VALUE_INVALID;
}
}

View File

@ -22,6 +22,7 @@ import org.apache.poi.ss.formula.TwoDEval;
import org.apache.poi.ss.formula.eval.*;
import org.apache.poi.ss.formula.functions.FreeRefFunction;
import org.apache.poi.ss.formula.functions.LookupUtils;
import org.apache.poi.util.ExceptionUtil;
/**
* Implementation of Excel function XMATCH()
@ -64,6 +65,9 @@ final class XMatchFunction implements FreeRefFunction {
} catch (EvaluationException e) {
return e.getErrorEval();
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
return ErrorEval.VALUE_INVALID;
}
}
@ -76,6 +80,9 @@ final class XMatchFunction implements FreeRefFunction {
} catch (EvaluationException e) {
return e.getErrorEval();
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
return ErrorEval.VALUE_INVALID;
}
}

View File

@ -22,6 +22,7 @@ import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.RefListEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.formula.ptg.NumberPtg;
import org.apache.poi.util.ExceptionUtil;
/**
* Returns the number of areas in a reference. An area is a range of contiguous cells or a single cell.
@ -42,6 +43,9 @@ public final class Areas implements Function {
}
return new NumberEval(new NumberPtg(result));
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
return ErrorEval.VALUE_INVALID;
}

View File

@ -21,6 +21,7 @@ import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.EvaluationException;
import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.util.ExceptionUtil;
import java.util.List;
@ -57,6 +58,9 @@ public class Correl extends Fixed2ArgFunction {
} catch (EvaluationException e) {
return e.getErrorEval();
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
return ErrorEval.NA;
}
}

View File

@ -22,6 +22,7 @@ import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.EvaluationException;
import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.util.ExceptionUtil;
import java.util.List;
@ -57,6 +58,9 @@ public class Covar extends Fixed2ArgFunction implements FreeRefFunction {
} catch (EvaluationException e) {
return e.getErrorEval();
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
return ErrorEval.NA;
}
}

View File

@ -32,6 +32,7 @@ import org.apache.poi.ss.formula.eval.StringEval;
import org.apache.poi.ss.formula.eval.StringValueEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.util.NumberComparer;
import org.apache.poi.util.ExceptionUtil;
import org.apache.poi.util.Internal;
import org.apache.poi.util.LocaleUtil;
@ -136,6 +137,9 @@ public final class DStarRunner implements Function3Arg {
return e.getErrorEval();
}
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
if (!algorithm.allowEmptyMatchField()) {
return ErrorEval.VALUE_INVALID;
}

View File

@ -28,6 +28,7 @@ import org.apache.poi.ss.formula.eval.OperandResolver;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.util.DateParser;
import org.apache.poi.util.ExceptionUtil;
import org.apache.poi.util.LocaleUtil;
/**
@ -82,6 +83,9 @@ public class Days implements FreeRefFunction {
double d0 = NumericFunction.singleOperandEvaluate(ve, srcRowIndex, srcColumnIndex);
return getDate(d0);
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
String strText1 = OperandResolver.coerceValueToString(ve);
return DateParser.parseLocalDate(strText1);
}

View File

@ -22,6 +22,7 @@ import org.apache.poi.ss.formula.eval.EvaluationException;
import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.OperandResolver;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.util.ExceptionUtil;
import java.util.List;
@ -73,6 +74,9 @@ public class Forecast extends Fixed3ArgFunction implements FreeRefFunction {
} catch (EvaluationException e) {
return e.getErrorEval();
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
return ErrorEval.NA;
}
}

View File

@ -31,6 +31,7 @@ import org.apache.poi.ss.formula.eval.OperandResolver;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.formula.ptg.Area3DPxg;
import org.apache.poi.ss.usermodel.Table;
import org.apache.poi.util.ExceptionUtil;
/**
* Implementation for Excel function INDIRECT<p>
@ -142,6 +143,9 @@ public final class Indirect implements FreeRefFunction {
try {
return ec.getDynamicReference(workbookName, sheetName, refStrPart1, refStrPart2, isA1style);
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
LOGGER.atWarn().log("Indirect function: failed to parse reference {}", text, e);
return ErrorEval.REF_INVALID;
}

View File

@ -25,6 +25,7 @@ import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.EvaluationException;
import org.apache.poi.ss.formula.eval.OperandResolver;
import org.apache.poi.util.ExceptionUtil;
import org.apache.poi.util.LocaleUtil;
/**
@ -111,6 +112,9 @@ public final class NumberValueFunction implements FreeRefFunction {
} catch (EvaluationException e) {
return e.getErrorEval();
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
return ErrorEval.VALUE_INVALID; //If any of the arguments are not valid, NUMBERVALUE returns the #VALUE! error value.
}

View File

@ -22,6 +22,7 @@ import org.apache.logging.log4j.Logger;
import org.apache.poi.ss.formula.eval.*;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.util.DateParser;
import org.apache.poi.util.ExceptionUtil;
import java.time.DateTimeException;
import java.time.LocalDate;
@ -60,11 +61,17 @@ public class TimeValue extends Fixed1ArgFunction {
try {
return parseTimeFromDateTime(dateTimeText);
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
try {
//this could be a time (with no date part) - prepend a dummy date because
//parseTimeFromDateTime needs it
return parseTimeFromDateTime("1/01/2000 " + dateTimeText);
} catch (Exception e2) {
if (ExceptionUtil.isFatal(e2)) {
ExceptionUtil.rethrow(e2);
}
LocalDate ld = DateParser.parseLocalDate(dateTimeText);
//return 0 as this is a pure date with no time element
return new NumberEval(0);

View File

@ -33,6 +33,7 @@ import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.OperandResolver;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.util.ExceptionUtil;
/**
* Implementation for Excel WeekNum() function.
@ -69,6 +70,9 @@ public class WeekNum extends Fixed2ArgFunction implements FreeRefFunction {
.atZone(ZoneId.systemDefault())
.toLocalDate();
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
return ErrorEval.NUM_ERROR;
}
int returnType;

View File

@ -47,6 +47,7 @@ import org.apache.poi.ss.format.CellFormatResult;
import org.apache.poi.ss.formula.ConditionalFormattingEvaluator;
import org.apache.poi.ss.util.DateFormatConverter;
import org.apache.poi.ss.util.NumberToTextConverter;
import org.apache.poi.util.ExceptionUtil;
import org.apache.poi.util.LocaleUtil;
import org.apache.poi.util.StringUtil;
@ -407,6 +408,9 @@ public class DataFormatter {
// Wrap and return (non-cacheable - CellFormat does that)
return new CellFormatResultWrapper( cfmt.apply(cellValueO) );
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
LOG.atWarn().withThrowable(e).log("Formatting failed for format {}, falling back", formatStr);
}
}
@ -1119,6 +1123,9 @@ public class DataFormatter {
try {
cellType = cell.getCachedFormulaResultType();
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
return cell.getCellFormula();
}
} else {

View File

@ -38,6 +38,7 @@ import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.ExceptionUtil;
import org.apache.poi.util.Internal;
@ -192,6 +193,9 @@ public class SheetUtil {
try {
sval = formatter.formatCellValue(cell, dummyEvaluator);
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
sval = String.valueOf(cell.getNumericCellValue());
}
} else if (cellType == CellType.BOOLEAN) {

View File

@ -34,6 +34,9 @@ public class Configurator {
try {
return Integer.parseInt(property);
} catch (Exception e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
LOG.atError().log("System property -D{} does not contains a valid integer: {}", systemProperty, property);
return defaultValue;
}

View File

@ -0,0 +1,60 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.util;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class ExceptionUtilTest {
@Test
void testRuntimeException() {
assertFalse(ExceptionUtil.isFatal(new RuntimeException("runtime issue")));
}
@Test
void testPlainException() {
assertFalse(ExceptionUtil.isFatal(new Exception("plain issue")));
}
@Test
void testOutOfMemoryError() {
assertTrue(ExceptionUtil.isFatal(new OutOfMemoryError("oom")));
}
@Test
void testVirtualMachineError() {
assertTrue(ExceptionUtil.isFatal(new VirtualMachineError(){}));
}
@Test
void testThreadDeath() {
assertTrue(ExceptionUtil.isFatal(new ThreadDeath()));
}
@Test
void testInterruptedException() {
assertTrue(ExceptionUtil.isFatal(new InterruptedException()));
}
@Test
void testLinkageError() {
assertTrue(ExceptionUtil.isFatal(new LinkageError()));
}
}