- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这段代码:
public class Resource implements AutoCloseable{
private String s = "I am resource.";
private int NuberOfResource;
public Resource(int NuberOfResource) {
this.NuberOfResource = NuberOfResource;
System.out.println(s + " My number is: " + NuberOfResource);
}
@Override
public void close() throws Exception {
System.out.println("Closing...");
}
public void print(){
System.out.println("Hello");
}
和主类:
public class Main {
public static void main(String[] args) {
int a, b = 0;
a = 5;
try {
Resource first = new Resurs(1);
Resource second = new Resurs(2);
System.out.println("I will cause exception");
a /= b;
} catch (Exception e) {
e.printStackTrace();
}
}
}
我想知道为什么我得到这个输出:
I am resource. My number is 1.
I am resource. My number is 2.
I will cause exception.
java.lang.ArithmeticException: / by zero
代替:
I am resource. My number is 1.
I am resource. My number is 2.
Closing...
Closing...
I will cause exception.
java.lang.ArithmeticException: / by zero
最佳答案
因为您没有使用try-with-resources
。 AutoCloseable
仅通过它调用,而不是通过常规的 try/catch
语句调用。
正确的模式是
try(Resource first = new Resource(1); Resource second = new Resource(2)) {
// .. whatever
}
关于java - Autocloseable 类不调用默认关闭方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35009153/
我有一个在构建对象时打开的资源。我用它在对象的整个生命周期中进行写入。但我的应用程序可能会在没有警告的情况下关闭,我需要捕获这一点。该类非常简单。 public class SomeWriter {
try-with-resources 很好,但在我看来,在创建包装多个 AutoCloseable 的类时,它仍然不足以进行有效的资源管理对象。例如,考虑 import java.io.*; clas
我试图理解以下代码片段。我知道 AutoClosable 接口(interface)定义了 close() 方法。 protected AutoCloseable fooAction() {
我有这段代码: public class Resource implements AutoCloseable{ private String s = "I am resource.";
所以我已经阅读了有关自动关闭界面的所有内容并尝试使用资源,但这让我想知道: 如果我不(忘记)包装一个实现 AutoCloseable 的类并尝试在使用该类的代码中使用资源,但该类确实从操作系统获取资源
从 Java 7 开始,Closeable接口(interface)已 retrofit 扩展AutoCloseable接口(interface),以便所有类实现 Closeable接口(interf
前面:我现在是一名 C# 人员,正在从事 Java 工作。其中一些挫折来自于我对 Java 的缺乏了解。 首先,有没有一种方法可以实现不需要检查Exception的“安全”AutoCloseable实
关闭 OutputStream 时遵循的最可靠模式是什么? , ServerSocket , 或其他实现 AutoCloseable 的对象界面? 我应该使用 try - catch - finall
我最近查看了 jdk 1.8 的 src.zip 文件,发现了一些很奇怪的东西。 public interface Closeable extends AutoCloseable 既然AutoClos
我想要的是一个 2 秒后自动关闭的成功模式。当我使用delay()时,这并不重要。打开后立即关闭。 $('.ui.modal.success') .modal('show') .de
Autocloseable应始终与 try-with-resources 一起使用.至少 Intellij 检查表明它。所以,如果我有一个生成 Foo 的代码实现 Autocloseable我应该这样
当你想使用一些 AutoClosable 对象时,你应该使用 try-with-resources .行。但是,如果我想编写返回 AutoClosable 的方法怎么办?在您创建或从某处接收到一个 A
考虑以下 try-with-resources block : try (Foo foo = getAFoo()) { } 对于某些实现了 java.lang.AutoCloseable 的类 Foo
try-with 功能如何对声明为 null 的 AutoCloseable 变量起作用? 我认为当它尝试在变量上调用 close 时这会导致空指针异常,但它运行没有问题: try (Buffered
长话短说:有没有办法发现,JVM 关闭仅在我的代码启动的线程中被阻止?例如,是否可以在关机时自动触发 AutoCloseable.close()? 上下文 我正在构建一个库,应该供多个客户使用。这意味
JDK 或通用库中是否有任何辅助方法可以执行此操作: if (resource instanceof AutoCloseable) { ((AutoCloseable) resource).c
我正在升级一些现有的 API,这些 API 将 Iterable 设为 AutoCloseable 敏感。例如,给定: /** * @throws NoSuchElementException *
当实现 AutoCloseable 以使用 Java 7 try-with-resources 语句时,我想知道 try block 中是否有异常。例如: class C implements Aut
我想构建一个自定义的 AutoCloseable 类,这样我就可以将其转换为: try { begin(); doThings(); commit(); } finally {
当我使用 sleep 定时器关闭窗口时,文本“lblDate.Content”消失了。如何显示该文本?显示窗口/文本的其余部分。我愿意接受其他自动关闭窗口的方法。 public void Displa
我是一名优秀的程序员,十分优秀!