Exceptions from both try block and try-with-resources

huangapple go评论61阅读模式
英文:

Exceptions from both try block and try-with-resources

问题

Documentation 中提到:
> 但在这个例子中,如果 readLineclose 方法都抛出异常,那么 readFirstLineFromFileWithFinallyBlock 方法会抛出从 finally 块抛出的异常;try 块抛出的异常会被抑制。相比之下,在例子 readFirstLineFromFile 中,如果异常同时从 try 块和 try-with-resources 语句中抛出,那么 readFirstLineFromFile 方法会抛出从 try 块抛出的异常;try-with-resources 语句中抛出的异常会被抑制。在 Java SE 7 及更高版本中,您可以检索被抑制的异常;有关更多信息,请参阅“被抑制的异常”部分。

我不理解加粗部分
> ... 如果异常同时从 try 块和 try-with-resources 语句中抛出 ...

如何同时从 try-with-resources 语句和 try 块抛出异常?如果异常是从 try-with-resources 语句中抛出的,那意味着资源初始化失败。在这种情况下,try 块永远不会执行。因此,前面的语句不可能发生。

我一定误解了这个 文档try-with-resources 的工作方式。您能提供一个加粗语句实际发生的示例吗?

<hr>
提到的方法

static String readFirstLineFromFile(String path) throws IOException {
    try (BufferedReader br =
                   new BufferedReader(new FileReader(path))) {
        return br.readLine();
    }
}
static String readFirstLineFromFileWithFinallyBlock(String path)
                                                     throws IOException {
    BufferedReader br = new BufferedReader(new FileReader(path));
    try {
        return br.readLine();
    } finally {
        if (br != null) br.close();
    }
}
英文:

Documentation says
> However, in this example, if the methods readLine and close both throw exceptions, then the method readFirstLineFromFileWithFinallyBlock throws the exception thrown from the finally block; the exception thrown from the try block is suppressed. In contrast, in the example readFirstLineFromFile, if exceptions are thrown from both the try block and the try-with-resources statement, then the method readFirstLineFromFile throws the exception thrown from the try block; the exception thrown from the try-with-resources block is suppressed. In Java SE 7 and later, you can retrieve suppressed exceptions; see the section Suppressed Exceptions for more information.

I don't understand the bold part
> ... if exceptions are thrown from both the try block and the try-with-resources statement ...

How can an exception be thrown from both the try-with-resources statement and the try block ? If the exception is thrown from the try-with-resources statement, it means that resource initialization failed. In this case, try block is never executed. Thus the previous statement can't happen.

I must have misunderstood this documentation and how try-with-resources works. Can you provide example where the bold statement actually happens ?

<hr>
Mentioned methods

static String readFirstLineFromFile(String path) throws IOException {
    try (BufferedReader br =
                   new BufferedReader(new FileReader(path))) {
        return br.readLine();
    }
}
static String readFirstLineFromFileWithFinallyBlock(String path)
                                                     throws IOException {
    BufferedReader br = new BufferedReader(new FileReader(path));
    try {
        return br.readLine();
    } finally {
        if (br != null) br.close();
    }
}

答案1

得分: 2

如果异常从try-with-resources语句和try块都被抛出,那么这意味着资源初始化失败。try-with-resources语句不仅初始化资源,还关闭资源,关闭可能会引发异常。这句话紧随对使用try-finally时类似情况的描述,并将其与try-with-resources进行比较。

英文:

> How can an exception be thrown from both the try-with-resources statement and the try block ? If the exception is thrown from the try-with-resources statement, it means that resource initialization failed.

The try-with-resources statement not only initializes but also closes the resource, and closing may throw an exception.

This sentence comes right after a description of a similar situation when using try-finally, and compares it with try-with-resources.

答案2

得分: 0

Oracle文档不够好。我不会将"try-with-resources"块称为隐式关闭方法,而是称其为构造函数,文档没有提及构造函数中的异常。

英文:

oracle documentation sucks. i wouldn't call try-with-resources block for implicit close methods, but rather for constructors and doc doesn't say anything about exception in constructors

huangapple
  • 本文由 发表于 2020年8月13日 01:54:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63382197.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定