Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';不要使用try-with-resources或finally-block_Java_Try Catch - Fatal编程技术网

Java Can';不要使用try-with-resources或finally-block

Java Can';不要使用try-with-resources或finally-block,java,try-catch,Java,Try Catch,我的代码正在从web上读取HTML页面,我想编写好的代码,所以我想使用try with resources或finally block关闭资源 使用下面的代码,似乎不可能使用它们中的任何一个来关闭“in” 您可以使用try with resources或finally编写相同的代码吗?我看不出以下代码有什么特别的困难: try (BufferedReader in = new BufferedReader(new InputStreamReader( new UR

我的代码正在从web上读取HTML页面,我想编写好的代码,所以我想使用try with resources或finally block关闭资源

使用下面的代码,似乎不可能使用它们中的任何一个来关闭“in”


您可以使用try with resources或finally编写相同的代码吗?

我看不出以下代码有什么特别的困难:

    try (BufferedReader in = new BufferedReader(new InputStreamReader(
            new URL("myurl").openStream()))) {

        String line = "";
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

这不是你想要的吗?

我看不出以下方面有什么特别的困难:

    try (BufferedReader in = new BufferedReader(new InputStreamReader(
            new URL("myurl").openStream()))) {

        String line = "";
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

这不是你想要的吗?

不相关的问题:为什么要将
IOException
转换为
RuntimeException
?如果要将其更改为
RuntimeException
,请确保包含原始异常:
抛出新的RuntimeException(e)IOException
转换为
RuntimeException
?如果要将其更改为
RuntimeException
,请确保包含原始异常:
抛出新的RuntimeException(e)