问题与练习
Questions
- 以下代码合法吗?
try {
} finally {
}
- 以下处理程序可以catch哪些异常类型?
catch (Exception e) {
}
使用这种类型的异常处理程序有什么问题?
- 编写的以下异常处理程序有什么问题吗?这段代码可以编译吗?
try {
} catch (Exception e) {
} catch (ArithmeticException a) {
}
-
将第一个列表中的每种情况与第二个列表中的项目进行匹配。
-
int[] A; A[0] = 0;
-
JVM 开始运行您的程序,但是 JVM 找不到 Java 平台类。 (Java 平台类位于
classes.zip
或rt.jar
中.) -
程序正在读取流并到达
end of stream
标记。 -
在关闭流之前和到达
end of stream
标记之后,程序将try再次读取流。
-
-
__error
-
__checked exception
-
__compile error
-
__no exception
-
Exercises
-
向ListOfNumbers.java添加
readList
方法。此方法应从文件中读取int
个值,打印每个值,并将它们附加到向量的末尾。您应该catch所有适当的错误。您还需要一个包含数字的文本文件才能读入。- 修改以下
cat
方法,使其可以编译。
- 修改以下
public static void cat(File file) {
RandomAccessFile input = null;
String line = null;
try {
input = new RandomAccessFile(file, "r");
while ((line = input.readLine()) != null) {
System.out.println(line);
}
return;
} finally {
if (input != null) {
input.close();
}
}
}