3

概括

我有一个可关闭的类型,CloseableClass它可以在其构造函数、方法甚至可能在内部抛出 IOError close。我想使用 try-with-resources 并且仍然以与使用期间的错误不同的方式处理构建期间的错误(使用包括清理)。更好的是,我想编写可维护的代码。


假设您希望构造一个可关闭的类实例并将其与 try-with-resources 语句一起使用。它可以抛出IOException它的构造函数和 try-with-resources 主体中使用的方法:

import java.io.Closeable;
import java.io.IOException;
import java.util.Random;

public class CloseableClass implements Closeable {
    public CloseableClass() throws IOException {
        if (new Random().nextBoolean()) {
            throw new IOException();
        }
    }

    public void internetStuff() throws IOException {
        if (new Random().nextBoolean()) {
            throw new IOException();
        }
    }

    public void close() throws IOException {
        if (new Random().nextBoolean()) {
            throw new IOException();
        }
    }

    public static void main(String[] args) {
        try (CloseableClass closeable = new CloseableClass()) {
            closeable.internetStuff();
        }
        catch (IOException e) {
            System.out.println("Bad error!");
        }
    }
}

假设您要分别处理构造函数和正文中抛出的错误。有支持的方法吗?在 Python 中,我会这样做:

try:
    closeable = CloseableClass()
except IOException:
    print("Constructor error")
    return

try:
    with closeable:
        closeable.internet_stuff()
except IOException:
    print("Body error")

但在 Java 中,你不能不为对象分配第二个名称:

CloseableClass closeable_;

try {
    closeable_ = new CloseableClass();
}
catch (IOException e) {            
    System.out.println("Constructor error!");
    return;
}

try (CloseableClass closeable = closeable_) {
    closeable.internetStuff();
}
catch (IOException e) {
    System.out.println("Body error!");
}

有人告诉我,这是“不可维护的代码”,主要是因为使用了closeable_,我离同意也不远了。我希望避免使用 try-finally ,因为那样你会遇到更糟糕的模拟它的问题:

CloseableClass closeable;

try {
    closeable = new CloseableClass();
}
catch (IOException e) {            
    System.out.println("Constructor error!");
    return;
}

try {
    closeable.internetStuff();
}
catch (IOException e) {
    try {
        closeable.close();
    }
    catch (IOException ignore) {
        // Already dealing with this
    }

    System.out.println("Body error!");
}
finally {
    try {
        closeable.close();
    }
    catch (IOException e) {
        System.out.println("Body error!");
    }
}

请注意,这需要第二次调用才能close成为无操作,测试类不遵守(请注意,AutoCloseable不需要这样做,尽管Closeable确实如此)。close当不能扔时,这有点好,但不多。

基本上问题是

  • close可以扔
  • 处理前关闭IOException以防止打印"Body error!"两次
  • 如何使它与来自 try-with-resources 的多个初始化程序一起工作并不明显
  • 无论如何,您最终都会复制代码。

我只是被迫忍受“不可维护的代码”还是我忽略了处理这个问题的好方法?

4

2 回答 2

1

'请注意,这需要第二次调用 close 才能成为无操作' - 不,您不需要close()catch块中,因为finally块将始终被执行。如果您使用类似in块的调用终止 JVM,您将只使用close()内部块。通常你会从时钟向调用者抛出,但你会在大部分时间块中执行清理。catchSystem.exit()catchExceptioncatchfinally

Try-with-resource 更好,但您可以使用 throwed 的类型和描述Exception来破译出错的原因和位置。

编辑

据我所知,我建议:

1)尝试资源:

try(Resource resource = new Resource()){
    // use resource.
}catch(Exception e){
    // handle exception.
    // OR better to throw exception to caller.
    throw e;
}

2) 常规样式:

Resource resource = null;
try{
    resource = new Resource();
    // use resource
}catch(Exception e){
    // handle exception.
    // OR better to throw exception to caller.
    throw e;
} finally {
   if(resource != null){
       try{
           resource.close();
       } catch(Exception e){
           // most of time you wont or cant do anything here.
       }
   }
}
于 2014-11-15T07:48:41.313 回答
1

一种解决方案是定义一种方法,该方法将初始化错误包装在自定义异常类型中,然后使用它来确定何时发生错误。

private CloseableClass createCloseable() throws CloseableCreateException{
    try {
        return new CloseableClass();
    } except (IOException e) {
        throw new CloseableCreateException(e);
    }
}
try (CloseableClass closeable = initCloseable()) {
    closeable.internetStuff();
} catch (CloseableCreateException e) {
    System.out.println("Constructor error!");
} catch (IOException e) {
    System.out.println("Body error!");
}

另一个简单但有点不优雅的解决方案是使用布尔标志:

boolean init = true;
try (CloseableClass closeable = new CloseableClass()) {
    init = false;
    closeable.internetStuff();
} catch (IOException e) {
    if (init) {
        System.out.println("Constructor error!");
    } else {
        System.out.println("Body error!");
    }
}
于 2019-06-18T19:01:48.237 回答