public final class EvaluationException
extends java.lang.Exception
Here is an example coded without EvaluationException, to show how it can help:
public Eval evaluate(Eval[] args, int srcRow, short srcCol) { // ... Eval arg0 = args[0]; if(arg0 instanceof ErrorEval) { return arg0; } if(!(arg0 instanceof AreaEval)) { return ErrorEval.VALUE_INVALID; } double temp = 0; AreaEval area = (AreaEval)arg0; ValueEval[] values = area.getValues(); for (int i = 0; i < values.length; i++) { ValueEval ve = values[i]; if(ve instanceof ErrorEval) { return ve; } if(!(ve instanceof NumericValueEval)) { return ErrorEval.VALUE_INVALID; } temp += ((NumericValueEval)ve).getNumberValue(); } // ... }In this example, if any error is encountered while processing the arguments, an error is returned immediately. This code is difficult to refactor due to all the points where errors are returned.
public Eval evaluate(Eval[] args, int srcRow, short srcCol) { try { // ... AreaEval area = getAreaArg(args[0]); double temp = sumValues(area.getValues()); // ... } catch (EvaluationException e) { return e.getErrorEval(); } } private static AreaEval getAreaArg(Eval arg0) throws EvaluationException { if (arg0 instanceof ErrorEval) { throw new EvaluationException((ErrorEval) arg0); } if (arg0 instanceof AreaEval) { return (AreaEval) arg0; } throw EvaluationException.invalidValue(); } private double sumValues(ValueEval[] values) throws EvaluationException { double temp = 0; for (int i = 0; i < values.length; i++) { ValueEval ve = values[i]; if (ve instanceof ErrorEval) { throw new EvaluationException((ErrorEval) ve); } if (!(ve instanceof NumericValueEval)) { throw EvaluationException.invalidValue(); } temp += ((NumericValueEval) ve).getNumberValue(); } return temp; }It is not mandatory to use EvaluationException, doing so might give the following advantages:
Note - Only standard evaluation errors are represented by EvaluationException ( i.e. conditions expected to be encountered when evaluating arbitrary Excel formulas). Conditions that could never occur in an Excel spreadsheet should result in runtime exceptions. Care should be taken to not translate any POI internal error into an Excel evaluation error code.
(此类用于简化运算符和函数实现中的错误处理逻辑。注意 - OperationEval.evaluate() 和 Function.evaluate() 方法签名不会引发此异常,因此它不能传播到外部。下面是一个没有EvaluationException的例子,展示了它如何提供帮助: public Eval evaluate(Eval[] args, int srcRow, short srcCol) { // ... Eval arg0 = args[0]; if(arg0 instanceof ErrorEval) { return arg0; } if(!(arg0 instanceof AreaEval)) { return ErrorEval.VALUE_INVALID; } 双温度 = 0; AreaEval 面积 = (AreaEval)arg0; ValueEval[] 值 = area.getValues(); for (int i = 0; i < values.length; i++) { ValueEval ve = values[i]; if(ve instanceof ErrorEval) { return ve; } if(!(ve instanceof NumericValueEval)) { return ErrorEval.VALUE_INVALID; } temp += ((NumericValueEval)ve).getNumberValue(); } // ... } 在此示例中,如果在处理参数时遇到任何错误,则会立即返回错误。由于返回错误的所有点,此代码难以重构。使用 EvaluationException 允许将错误返回代码合并到一个位置。 public Eval evaluate(Eval[] args, int srcRow, short srcCol) { try { // ... AreaEval area = getAreaArg(args[0]);双温度 = sumValues(area.getValues()); // ... } catch (EvaluationException e) { return e.getErrorEval(); } } private static AreaEval getAreaArg(Eval arg0) throws EvaluationException { if (arg0 instanceof ErrorEval) { throw new EvaluationException((ErrorEval) arg0); } if (arg0 instanceof AreaEval) { return (AreaEval) arg0; } throw EvaluationException.invalidValue(); } private double sumValues(ValueEval[] values) throws EvaluationException { double temp = 0; for (int i = 0; i < values.length; i++) { ValueEval ve = values[i]; if (ve instanceof ErrorEval) { throw new EvaluationException((ErrorEval) ve); } if (!(ve instanceof NumericValueEval)) { throw EvaluationException.invalidValue(); } temp += ((NumericValueEval) ve).getNumberValue(); } 返回温度; } 使用 EvaluationException 不是强制性的,这样做可能会带来以下好处: - 可以更容易地提取方法,允许重用。 - 类型管理(类型转换等)更简单,因为错误条件已与中间计算值分开。 - 需要更少的局部变量。局部变量可以有更强的类型。 - 更容易模仿常见的 Excel 错误处理行为(遇到第一个错误时退出),因为无论执行点或嵌套调用的级别数如何,异常都可以方便地向上传播调用堆栈。注意 - 仅标准评估错误由 EvaluationException 表示(即在评估任意 Excel 公式时预期会遇到的条件)。 Excel 电子表格中永远不会出现的情况应该会导致运行时异常。应注意不要将任何 POI 内部错误转换为 Excel 评估错误代码。)Constructor and Description |
---|
EvaluationException(ErrorEval errorEval) |
Modifier and Type | Method and Description |
---|---|
ErrorEval |
getErrorEval() |
static EvaluationException |
invalidRef()
#REF! - Illegal or deleted cell reference
(#参考! - 非法或删除的单元格引用)
|
static EvaluationException |
invalidValue()
#VALUE! - Wrong type of operand
(#价值! - 错误类型的操作数)
|
static EvaluationException |
numberError()
#NUM! - Value range overflow
(#NUM! - 值范围溢出)
|
public EvaluationException(ErrorEval errorEval)
public static EvaluationException invalidValue()
public static EvaluationException invalidRef()
public static EvaluationException numberError()
public ErrorEval getErrorEval()
Copyright 2021 The Apache Software Foundation or its licensors, as applicable.