UriComponentsBuilder如何避免URISyntaxException?

时间:2018-06-01 14:06:02

标签: java spring uri

Spring org.springframework.web.util.UriComponentsBuilder和相关UriComponents.toUri()如何避免URISyntaxException构造函数发生的已检查异常java.net.URI

例如,使用new URI()会导致必须捕获已检查的异常:

try {
  uri = new URI(myUrl);
} catch (URISyntaxException e) {
  // oops
}

VS。使用Spring的构建器避免了:

uri = UriComponentsBuilder.fromUriString(myUrl)
  .build()
  .toUri();
// no checked exception here

1 个答案:

答案 0 :(得分:0)

啊,发现它,它们被捕获并转换为运行时IllegalStateException例外:

至于动机,Spring通常赞成运行时异常:why spring handles only unchecked exceptions