java.time包的一次踩坑报错仅clock/month/temporal等文案问题。

编程

 {
  "code" : 1,
  "msg" : "temporal",
  "data" : null
}

当时就懵逼过去。我们一般的异常都有断言去处理。这个报错意味着

e.getMessage() 仅仅是个 temporal

再修改报错返回发布之后获得了完整报错信息。

LocalDate.from(temporal);

// LocalDate 源码

public static LocalDate from(TemporalAccessor temporal) {

Objects.requireNonNull(temporal, "temporal");

LocalDate date = temporal.query(TemporalQueries.localDate());

if (date == null) {

throw new DateTimeException("Unable to obtain LocalDate from TemporalAccessor: " +

temporal + " of type " + temporal.getClass().getName());

}

return date;

}

这报错倒是简单了。但是碰到的人就懵逼了。java.time 包的都是这种写法。

对比下其他类的源码

// StringJoiner 源码

public StringJoiner(CharSequence delimiter,

CharSequence prefix,

CharSequence suffix) {

Objects.requireNonNull(prefix, "The prefix must not be null");

Objects.requireNonNull(delimiter, "The delimiter must not be null");

Objects.requireNonNull(suffix, "The suffix must not be null");

// make defensive copies of arguments

this.prefix = prefix.toString();

this.delimiter = delimiter.toString();

this.suffix = suffix.toString();

this.emptyValue = this.prefix + this.suffix;

}

坑呀

以上是 java.time包的一次踩坑报错仅clock/month/temporal等文案问题。 的全部内容, 来源链接: utcz.com/z/513900.html

回到顶部