繁体   English   中英

未报告的异常IOException从URL读取文件

[英]Unreported exception IOException reading file from URL

我到处都看过,还没有找到解决方案。 简而言之,我正在尝试从提供的URL中提取文本文件并将其读取为String,以便我可以使用它做其他事情。

具体的编译错误是:

 /tmp/java_kWWEO5/Main.java:49: error: unreported exception IOException; must be caught or declared to be thrown String text = readUrlTextContent("http://textfiles.com/stories/antcrick.txt"); ^ 1 error 

导致错误的代码,在main

import java.util.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {

    private static String readUrlTextContent(String url) throws IOException, MalformedURLException {

        URL source = new URL(url);
        BufferedReader reader = new BufferedReader(new InputStreamReader(source.openStream()));
        try {
            StringBuilder builder = new StringBuilder();
            String line = reader.readLine();

            while (line != null) {
                builder.append(line);
                builder.append("\n");
                line = reader.readLine();
            }
            return builder.toString();
        } catch (MalformedURLException urlEx) {
            urlEx.printStackTrace();
            throw new RuntimeException("Malformed URL Exception", urlEx);            
        } catch (IOException ioEx) {
            ioEx.printStackTrace();
            throw new RuntimeException("IO Exception", ioEx);
        } finally {
            reader.close();
        }
    }

    public static void main(String[] args) {

        String text = readUrlTextContent("http://textfiles.com/stories/antcrick.txt");
        System.out.println(text);
    }
}

我该怎么做才能修改这个简短的程序,使其最终编译并执行?

通过将它们包装到RuntimeException中来实际在方法的主体中处理这些异常,而方法签名则“依赖”编译器:

 private static String readUrlTextContent(String url) throws IOException, MalformedURLException { 

将此行更改为:

private static String readUrlTextContent(String url) {

那应该解决编译器消息


您在这里遇到的问题是检查异常所固有的。 当一个方法声明它抛出一个异常某些类型,编译器需要检查这些异常处理。

这意味着,即使您正确地捕获了方法主体中的异常,编译器也必须假设这些异常可能发生。

因此,无论何时reaadUrlTextContent ,编译器都会检查“调用方是否处理这些异常?”。

如果呼叫者没有...那就是您得到的:)

使您的案例更有趣的是, MalformedURLExceptionIOException的子类。 这意味着,每当您捕获(或抛出)IOException时,鉴于尚未捕获到MalformedURLException,都将以相同的方式进行处理。

如下更新readUrlTextContent(String url)的签名:

private static String readUrlTextContent(String url) throws IOException{
}

并且,修改main()如下:

public static void main(String[] args) {
    String text = null;
    try {
        text = readUrlTextContent("http://textfiles.com/stories/antcrick.txt");
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println(text);
}

或仅如下修改readUrlTextContent():

private static String readUrlTextContent(String url) {
        BufferedReader reader = null;
        try {
            URL source = new URL(url);


            reader = new BufferedReader(new InputStreamReader(
                    source.openStream()));

            StringBuilder builder = new StringBuilder();
            String line = reader.readLine();

            while (line != null) {
                builder.append(line);
                builder.append("\n");
                line = reader.readLine();
            }
            return builder.toString();
        } catch (MalformedURLException urlEx) {
            urlEx.printStackTrace();
            throw new RuntimeException("Malformed URL Exception", urlEx);
        } catch (IOException ioEx) {
            ioEx.printStackTrace();
            throw new RuntimeException("IO Exception", ioEx);
        } finally {
            try {
                if(null!=reader)
                    reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM