Tôi có thể "sửa" ngoại lệ dưới đây với vòng lặp cố gắng nhưng tôi không hiểu lý do.Java: IOEXceptions trong BufferedReader của readLine() cho là gì?
- Tại sao phần "in.readLine()" liên tục kích hoạt IOExceptions?
- Điều gì thực sự là mục đích của việc ném ngoại lệ như vậy, mục tiêu có lẽ không chỉ là nhiều tác dụng phụ?
Mã và 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);
}
}
}
Hướng dẫn ngoại lệ về mặt trời: http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html – BalusC