我有一个接收ArrayList
路由的构造函数:
public Voo(ArrayList<Routes> routes, LocalDateTime dateTime, Duration duration) {
if (routes.isEmpty()) {
}
this.routes = routes;
this.dateTime = dateTime;
this.duration = duration;
this.status = Status.CONFIRMED;
}
如果收到的Voo
为空,如何阻止构造函数返回routes
的实例?
答案 0 :(得分:2)
通常会抛出IllegalArgumentException
。 e.g。
if (routes.isEmpty()) {
throw new IllegalArgumentException("routes should be non-empty");
}