未报告的异常 IOException 从 URL 读取文件
Unreported exception IOException reading file from URL
我找遍了 SO,但还没有找到解决办法。总之,我正在尝试从提供的 URL 中获取文本文件并将其读入字符串,以便我可以用它做其他事情。
具体编译错误为:
/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 中来处理方法主体中的这些异常,但您对编译器的方法签名 "lies":
private static String readUrlTextContent(String url) throws IOException, MalformedURLException {
将此行更改为:
private static String readUrlTextContent(String url) {
这应该可以解决编译器消息
您在这里遇到的问题是检查异常所固有的。当一个方法声明它抛出某种异常类型时,编译器需要检查这些异常是否得到处理。
这意味着,即使您在方法主体中正确捕获了异常,编译器也必须假定,这些异常可能会发生。
因此,每当调用 reaadUrlTextContent
时,编译器都会检查 "Does the caller handle these exceptions?"
如果来电者不...那么这就是你得到的:)
使您的案例更有趣的是 MalformedURLException
是 IOException
的子类。这意味着每当您捕获(或抛出)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();
}
}
}
我找遍了 SO,但还没有找到解决办法。总之,我正在尝试从提供的 URL 中获取文本文件并将其读入字符串,以便我可以用它做其他事情。
具体编译错误为:
/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 中来处理方法主体中的这些异常,但您对编译器的方法签名 "lies":
private static String readUrlTextContent(String url) throws IOException, MalformedURLException {
将此行更改为:
private static String readUrlTextContent(String url) {
这应该可以解决编译器消息
您在这里遇到的问题是检查异常所固有的。当一个方法声明它抛出某种异常类型时,编译器需要检查这些异常是否得到处理。
这意味着,即使您在方法主体中正确捕获了异常,编译器也必须假定,这些异常可能会发生。
因此,每当调用 reaadUrlTextContent
时,编译器都会检查 "Does the caller handle these exceptions?"
如果来电者不...那么这就是你得到的:)
使您的案例更有趣的是 MalformedURLException
是 IOException
的子类。这意味着每当您捕获(或抛出)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();
}
}
}