在try catch块中处理MalformedURLException异常

时间:2018-03-23 10:57:03

标签: java exception exception-handling

我有一段代码从给定的网址下载文件。我使用InputStream从流中读取数据。

public static byte[] fetchAttachmentFromThisURL(String path) {
    String urlString = ORBEON_BASE + path;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    URL obj = new URL(urlString);
    InputStream is = obj.openStream();
    int n;
    byte[] byteChunk = new byte[4096];
    while ((n = is.read(byteChunk)) > 0) {
        baos.write(byteChunk, 0, n);
    }
    is.close();
    return baos.toByteArray();
}

URL obj = new URL(urlString);抛出MalformedURLExceptionis.close();会抛出IOException

我的目标是处理方法fetchAttachmentFromThisURL中的所有异常。我不想在方法中使用throws关键字。

方法设计1:

public static byte[] fetchAttachmentFromThisURL(String path) {
    String urlString = ORBEON_BASE + path;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    InputStream is = null;
    try {
        URL obj = new URL(urlString);
        is = obj.openStream();
        int n;
        byte[] byteChunk = new byte[4096];
        while ((n = is.read(byteChunk)) > 0) {
            baos.write(byteChunk, 0, n);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        is.close();
    }
    return baos.toByteArray();
}

finally中,必须有另一个try-catch块来处理IOException,这绝对不是一种更干净的方法。

方法设计2:

public static byte[] fetchAttachmentFromThisURL(String path) {
    String urlString = ORBEON_BASE + path;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    URL obj = new URL(urlString);
    try (InputStream is = obj.openStream()) {
        int n;
        byte[] byteChunk = new byte[4096];
        while ((n = is.read(byteChunk)) > 0) {
            baos.write(byteChunk, 0, n);
        }
    } catch (IOException e) {
        e.printStackTrace();            
    }
    return baos.toByteArray();
}

这里,由于obj初始化抛出MalformedURLException,因此存在编译时错误。同样,我无法在try块中移动此初始化行,因为这样做会使obj不可见。

除非finally使用try-catch,否则必须在resource块中关闭输入流。

有没有办法实现上述目标(如果可能,只能使用一个try-catch块)?

2 个答案:

答案 0 :(得分:1)

方法1但没有finally来关闭流而不是try-with-resources语句 请注意,在is.close();引发NullPointerException的情况下,如果isnull,则代码IOException语句将抛出URL。 通过将流关闭任务留给JVM,您可以避免添加检查以及finally

public static byte[] fetchAttachmentFromThisURL(String path) {
    String urlString = ORBEON_BASE + path;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    try (InputStream is = new URL(urlString).openStream()) {                       
        int n;
        byte[] byteChunk = new byte[4096];
        while ((n = is.read(byteChunk)) > 0) {
            baos.write(byteChunk, 0, n);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } 
    return baos.toByteArray();
}

答案 1 :(得分:1)

Eclipse IDE提出了这种设计。我假设不应该有任何资源泄漏以及例外。

public static byte[] fetchAttachmentFromThisURL(String path) {
    String urlString = "" + path;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        URL obj = new URL(urlString);
        int n;
        byte[] byteChunk = new byte[4096];
        while ((n = obj.openStream().read(byteChunk)) > 0) {
            baos.write(byteChunk, 0, n);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return baos.toByteArray();
}