2

直接来自这个jls

带有声明多个资源的 ResourceSpecification 子句的 try-with-resources 语句被视为多个 try-with-resources 语句,每个语句都有一个声明单个资源的 ResourceSpecification 子句。翻译具有 n 个资源 (n > 1) 的 try-with-resources 语句时,结果是具有 n-1 个资源的 try-with-resources 语句。经过n次这样的翻译,有n个嵌套的try-catch-finally语句,整体翻译完成。

对我来说,上面引用的声明的最后一部分非常清楚。如果我像他们所说的那样声明了 n 个资源,为什么the result is a try-with-resources statement with n-1 Resources

为什么不使用 n 资源?

提前致谢。

4

3 回答 3

2

JLS 试图说的是,如果您有:

try (
   Closable closeable1 = ...;
   Closable closeable2 = ...;
   // ...
   Closable closeablen = ...;
) {
   // ...
}

它将被反复分解为:

try (Closable closeable1 = ...) {
   try (
      Closable closeable2 = ...;
      // ...
      Closable closeablen = ...;
   ) {
      // ...
   }
}

只要try. 这只是一个说明,因为我假设它被分解为用于 try-with-resources 的实际字节码。

于 2013-08-05T13:38:09.373 回答
1

这在使用 Java SE 7 实现更好的资源管理:超越语法糖中或多或少地得到了解释。以这个片段为例:

try(
    FileInputStream fin = new FileInputStream(input);
    FileOutputStream fout = new FileOutputStream(output);
    GZIPOutputStream out = new GZIPOutputStream(fout)
) {
    // ... do work
}

这将等同于以下内容:

FileInputStream localFileInputStream = new FileInputStream(paramString1);
Object localObject1 = null;
try {
    FileOutputStream localFileOutputStream = new FileOutputStream(paramString2);
    Object localObject2 = null;
    try {
        GZIPOutputStream localGZIPOutputStream = new GZIPOutputStream(localFileOutputStream);
        Object localObject3 = null;
            try {
                // ... do work
           } catch (Throwable localThrowable6) {
               localObject3 = localThrowable6;
               throw localThrowable6;
           } finally {
               if (localGZIPOutputStream != null) {
                   if (localObject3 != null) {
                       try {
                           localGZIPOutputStream.close();
                       } catch (Throwable localThrowable7) {
                           localObject3.addSuppressed(localThrowable7);
                       }
                   } else {
                       localGZIPOutputStream.close();
                   }
               }
           }
       } catch (Throwable localThrowable4) {
           localObject2 = localThrowable4;
           throw localThrowable4;
       } finally {
           if (localFileOutputStream != null) {
               if (localObject2 != null) {
                   try {
                       localFileOutputStream.close();
                   } catch (Throwable localThrowable8) {
                       localObject2.addSuppressed(localThrowable8);
                   }
               } else {
                   localFileOutputStream.close();
               }
           }
       }
   } catch (Throwable localThrowable2) {
       localObject1 = localThrowable2;
       throw localThrowable2;
   } finally {
       if (localFileInputStream != null) {
           if (localObject1 != null) {
               try {
                   localFileInputStream.close();
               } catch (Throwable localThrowable9) {
                   localObject1.addSuppressed(localThrowable9);
               }
           } else {
               localFileInputStream.close();
           }
       }
   }
}

是的,那是一大堆代码,但正如您所见,它将每个try-with-resources 块嵌套在上面的块中。

于 2013-08-05T13:39:10.117 回答
1

我将此理解为一个声明

try (
    resource 1
    resource 2
    resource n
) { 
}

在语句中递归翻译n,第一步是n-1在内部语句中使用资源:

try (
    resource 1
) {

    try ( // n-1 resources here
        resource 2
        resource n
    ) {
    }
 }

我同意编辑不清楚。

于 2013-08-05T13:39:28.660 回答