IntelliJ IDE gives error when using Try-Catch with Resources

前端 未结 5 683
栀梦
栀梦 2020-12-13 08:30

I am attempting to use JDK 7\'s \"try-catch with resources\" statement; IntelliJ highlights my resource line, saying

Try-with-resources are not supp

5条回答
  •  天命终不由人
    2020-12-13 09:23

    Also check your code. You might have accidentally did something like this:

    try (HttpClients.createMinimal().execute(new HttpGet(String.format(
              "http://127.0.0.1:%s/extra/LifecycleServlet?action=shutdown",
              runningPort)))) {
    

    instead of

    try (CloseableHttpResponse response = HttpClients.createMinimal().execute(new HttpGet(String.format(
              "http://127.0.0.1:%s/extra/LifecycleServlet?action=shutdown",
              runningPort)))) {
    

    easy mistake to make when you don't intend on using the result of your closeable resource. yet it will have that misleading error.

提交回复
热议问题