Description
Currently HiveDecimal class provided a bunch of constructors that unfortunately also throws a runtime exception. For example,
public HiveDecimal(BigInteger unscaled, int scale) { bd = this.normalize(new BigDecimal(unscaled, scale), MAX_PRECISION, false); if (bd == null) { throw new NumberFormatException("Assignment would result in truncation"); }
As a result, it's hard for the caller to detect error occurrences and the error handling is also complicated. In many cases, the error handling is omitted or missed. For instance,
HiveDecimalWritable result = new HiveDecimalWritable(HiveDecimal.ZERO); try { result.set(aggregation.sum.divide(new HiveDecimal(aggregation.count))); } catch (NumberFormatException e) { result = null; }
Throwing runtime exception while expecting caller to catch seems anti-pattern. In the case of constructor, factory class or methods seem more appropriate. With such a change, the apis are cleaner, and the error handling is simplified.