2

所以。我正在尝试在 Android 上做一些网络工作。在我的异步任务中,我正在做:

InputStream streamOfDestiny = null;

try{
    // do some network stuff here...
}
finally{
    if(streamOfDestiny != null){
        streamOfDestiny.close(); // Build error here. Apparently, closing a stream can cause an IOException. Why this is the case, I do not know. But it is. And, since this is Java, I apparently need to care.
    }
}

所以现在我有这个 IOException 把一切都搞砸了。我可以做这样的事情:

InputStream streamOfDestiny = null;

try{
    // do some network stuff here...
}
finally{
    if(streamOfDestiny != null){
        try{
            streamOfDestiny.close();
        }
        catch(IOException e){
            // Hey look! I'm inside a catch block, inside a finally block!
        }
    }
}

但这看起来很糟糕。finally 块中的 try/catch 块?多么丑陋!我完全可以让它保持关闭状态,但这对我来说似乎是一种不好的做法,而且感觉不对(我开始直播,我想完成它)。我可以这样做:

IOUtils.closeQuietly(streamOfDestiny);

但现在我必须找到 org.apache.commons.io.IOUtils 并以某种方式将其包含在我的包中。工作量太大,加上我只需要一个功能的东西增加了我的包大小。瘸。

我总是可以编写自己的 closeQuietly 版本:

public static void closeStreamQuietly(InputStream streamToClose){
    try{
        streamToClose.close();
    }
    catch (IOException e){
        // ignore it....
    }
}

但这似乎是我在重新发明轮子,这几乎总是坏消息——感觉应该有一些不错的、优雅的方式来做这件事,而我在这里完全错过了。

有什么想法吗?

4

4 回答 4

0

我不确定你为什么将你的 option-2(try-catch 块在 finally)或 option-4(创建一个小 util 方法)称为丑陋或额外的工作。这是正常的,也是意料之中的。

如果您在内部编写任何代码,finally则应进行相关的异常处理,这就是这种情况。

您已经通过null检查完成的一个异常处理(预防性),否则它可能会NullPointerExceptionstreamOfDestiny为空时抛出。

需要第二次异常处理来处理 Stream 关​​闭的异常场景,这可能是由于 Stream is 等原因not openunavailable或者像这样it's not able to release the underline resources的场景等。

于 2012-12-03T05:03:02.720 回答
0

直接从网上复制 IOUtils 代码:

http://grepcode.com/file/repo1.maven.org/maven2/commons-io/commons-io/1.4/org/apache/commons/io/IOUtils.java#IOUtils.closeQuietly%28java.io.InputStream% 29

此外,习惯于在 finally 块中处理异常。这不会是你最后一次看到它。

于 2012-12-03T05:25:10.157 回答
0

没有什么奥秘。close()在大多数实现中调用flush()(参见 Javadoc 的FilterOutputStream),并且flush()可以抛出IOException,,因为它可能执行 I/O。其他可能性是可以想象的。

于 2012-12-03T09:47:29.213 回答
0

您应该使用资源尝试。

try (InputStream streamOfDestiny = ...) {
    //Do you networking stuff
} catch (IOException ioe) {
    //Handle the exceptions
}
于 2018-03-09T23:28:03.827 回答