gpt4 book ai didi

In try with resources in java, what happens when there is an exception in close() when there is a catch block? [duplicate](在尝试使用Java中的资源时,如果Close()中有Catch块时出现异常,会发生什么情况?[复制])

转载 作者:bug小助手 更新时间:2023-10-28 22:36:00 25 4
gpt4 key购买 nike




In this code, when there is an exception in br.close(), will catch block catch it or will the process be terminated?

在这段代码中,当br.lose()中出现异常时,Catch块会捕获它还是会终止进程?


import java.io.BufferedReader;
import java.io.InputStreamReader;

public class TryWithBlock {
public static void main(String args[])
{
System.out.println("Enter a number");
try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in)))
{
int i = Integer.parseInt(br.readLine());
System.out.println(i);
}
catch(Exception e)
{
System.out.println("A wild exception has been caught");
System.out.println(e);
}
}
}

更多回答
优秀答案推荐

From the docs:

从文档中:



The resource declared in the try-with-resources statement is a
BufferedReader. The declaration statement appears within parentheses
immediately after the try keyword. The class BufferedReader, in Java
SE 7 and later, implements the interface java.lang.AutoCloseable.
Because the BufferedReader instance is declared in a try-with-resource
statement, it will be closed regardless of whether the try statement
completes normally or abruptly (as a result of the method
BufferedReader.readLine throwing an IOException).



Basically, it's equivalent to:

基本上,它相当于:


BufferedReader br = new BufferedReader(new FileReader(System.in));
try {
int i = Integer.parseInt(br.readLine());
System.out.println(i);
} finally {
br.close();
}

Let's try it out (from here a working example):

让我们试一试(从这里开始是一个有效的例子):


//class implementing java.lang.AutoCloseable
public class ClassThatAutoCloses implements java.lang.AutoCloseable {


public ClassThatAutoCloses() {}

public void doSomething() {

System.out.println("Pippo!");
}


@Override
public void close() throws Exception {

throw new Exception("I wasn't supposed to fail");

}

}

//the main class
public class Playground {

/**
* @param args
*/
public static void main(String[] args) {

//this catches exceptions eventually thrown by the close
try {

try(var v = new ClassThatAutoCloses() ){

v.doSomething();

}
} catch (Exception e) {
//if something isn't catched by try()
//close failed will be printed
System.err.println("close failed");
e.printStackTrace();
}
}

}

//the output
Pippo!
close failed
java.lang.Exception: I wasn't supposed to fail
at ClassThatAutoCloses.close(ClassThatAutoCloses.java:26)
at Playground.main(Playground.java:24)


Exceptions thrown inside the try-with-resources statement are supressed. Exceptions thrown inside the try block are propagated. So in your case the catch block will catch the parsing exception (if any).

Try-With-Resources语句中引发的异常被抑制。在Try块内引发的异常将被传播。因此,在您的例子中,Catch块将捕获解析异常(如果有的话)。


For more details you can refer at the docs.

有关更多详细信息,请参阅文档。


更多回答

The question seems not to be asking whether close() will be invoked in the event of an exception, but rather what happens if close() itself throws an exception.

问题似乎不是问在发生异常时是否调用Close(),而是问如果Close()本身抛出异常会发生什么。

In that case an exception will be thrown

在这种情况下,将引发异常

new FileReader(System.in) used in your first example is invalid

第一个示例中使用的新FileReader(system.In)无效

Why did you use a nested try block?

为什么要使用嵌套的Try块?

@KMURALIKRISHNA To catch the exception thrown by the implicit stop() call made by the resource try try( ){}.

@KMURALIKRISHNA捕获由资源try try(){}进行的隐式Stop()调用引发的异常。

OP was asking about exceptions from close().

OP正在询问Close()的异常。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com