我可以使用 try-catch 循环“修复”以下异常,但我无法理解原因。
- 为什么“in.readLine()”部分会持续引发 IOExceptions?
- 抛出此类异常的真正目的是什么,目标可能不仅仅是更多的副作用?
代码和 IOExceptions
$ javac ReadLineTest.java
ReadLineTest.java:9: unreported exception java.io.IOException; must be caught or declared to be thrown
while((s=in.readLine())!=null){
^
1 error
$ cat ReadLineTest.java
import java.io.*;
import java.util.*;
public class ReadLineTest {
public static void main(String[] args) {
String s;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
// WHY IOException here?
while((s=in.readLine())!=null){
System.out.println(s);
}
}
}