The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
The previous sections described how to construct the 前面的部分描述了如何在try, catch, and finally code blocks for the writeList method in the ListOfNumbers class.ListOfNumbers类中为writeList方法构造try、catch和finally代码块。Now, let's walk through the code and investigate what can happen.现在,让我们浏览一下代码并调查可能发生的情况。
When all the components are put together, the 将所有组件放在一起时,writeList method looks like the following.writeList方法如下所示。
public void writeList() {
PrintWriter out = null;
try {
System.out.println("Entering" + " try statement");
out = new PrintWriter(new FileWriter("OutFile.txt"));
for (int i = 0; i < SIZE; i++) {
out.println("Value at: " + i + " = " + list.get(i));
}
} catch (IndexOutOfBoundsException e) {
System.err.println("Caught IndexOutOfBoundsException: "
+ e.getMessage());
} catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
} finally {
if (out != null) {
System.out.println("Closing PrintWriter");
out.close();
}
else {
System.out.println("PrintWriter not open");
}
}
}As mentioned previously, this method's 如前所述,此方法的try block has three different exit possibilities; here are two of them.try块有三种不同的退出可能性;这里有两个。
try statement fails and throws an exception.try语句中的代码失败并引发异常。IOException caused by the new FileWriter statement or an IndexOutOfBoundsException caused by a wrong index value in the for loop.new FileWriter语句引起的IOException,也可能是由for循环中的错误索引值引起的IndexOutOfBoundsException。try statement exits normally.try语句正常退出。Let's look at what happens in the 让我们看看writeList method during these two exit possibilities.writeList方法在这两种退出可能性期间发生了什么。
The statement that creates a 创建FileWriter can fail for a number of reasons.FileWriter的语句可能会失败,原因有很多。For example, the constructor for the 例如,如果程序无法创建或写入指定的文件,FileWriter throws an IOException if the program cannot create or write to the file indicated.FileWriter的构造函数将抛出IOException。
When 当FileWriter throws an IOException, the runtime system immediately stops executing the try block; method calls being executed are not completed.FileWriter抛出IOException时,运行时系统立即停止执行try块;正在执行的方法调用未完成。The runtime system then starts searching at the top of the method call stack for an appropriate exception handler.然后,运行时系统开始在方法调用堆栈的顶部搜索适当的异常处理程序。In this example, when the 在本例中,当IOException occurs, the FileWriter constructor is at the top of the call stack.IOException发生时,FileWriter构造函数位于调用堆栈的顶部。However, the 但是,FileWriter constructor doesn't have an appropriate exception handler, so the runtime system checks the next method the writeList method in the method call stack.FileWriter构造函数没有适当的异常处理程序,因此运行时系统会在方法调用堆栈中检查下一个方法writeList方法。The writeList method has two exception handlers: one for IOException and one for IndexOutOfBoundsException.writeList方法有两个异常处理程序:一个用于IOException,另一个用于IndexOutOfBoundsException。
The runtime system checks 运行时系统按照writeList's handlers in the order in which they appear after the try statement.writeList的处理程序在try语句之后出现的顺序检查它们。The argument to the first exception handler is 第一个异常处理程序的参数是IndexOutOfBoundsException.IndexOutOfBoundsException。This does not match the type of exception thrown, so the runtime system checks the next exception handler 这与引发的异常类型不匹配,因此运行时系统检查下一个异常处理程序IOException.IOException。This matches the type of exception that was thrown, so the runtime system ends its search for an appropriate exception handler.这与抛出的异常类型匹配,因此运行时系统结束对适当异常处理程序的搜索。Now that the runtime has found an appropriate handler, the code in that 现在运行库已经找到了一个合适的处理程序,该catch block is executed.catch块中的代码就被执行了。
After the exception handler executes, the runtime system passes control to the 执行异常处理程序后,运行时系统将控制权传递给finally block.finally块。Code in the finally block executes regardless of the exception caught above it.finally块中的代码将执行,而不考虑上面捕获的异常。In this scenario, the 在这种情况下,FileWriter was never opened and doesn't need to be closed.FileWriter从未打开,也不需要关闭。After the 在finally block finishes executing, the program continues with the first statement after the finally block.finally块完成执行后,程序继续执行finally块后的第一条语句。
Here's the complete output from the 下面是ListOfNumbers program that appears when an IOException is thrown.ListOfNumber程序的完整输出,该程序在引发IOException时显示。
Entering try statement Caught IOException: OutFile.txt PrintWriter not open
The boldface code in the following listing shows the statements that get executed during this scenario:下表中的粗体代码显示了在此场景中执行的语句:
public void writeList() {
PrintWriter out = null;
try {
System.out.println("Entering try statement");
out = new PrintWriter(new FileWriter("OutFile.txt"));
for (int i = 0; i < SIZE; i++)
out.println("Value at: " + i + " = " + list.get(i));
} catch (IndexOutOfBoundsException e) {
System.err.println("Caught IndexOutOfBoundsException: "
+ e.getMessage());
} catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
} finally {
if (out != null) {
System.out.println("Closing PrintWriter");
out.close();
}
else {
System.out.println("PrintWriter not open");
}
}
}In this scenario, all the statements within the scope of the 在这种情况下,try block execute successfully and throw no exceptions.try块范围内的所有语句都会成功执行,并且不会抛出异常。Execution falls off the end of the 执行从try block, and the runtime system passes control to the finally block.try块的末尾开始,运行时系统将控制权传递给finally块。Because everything was successful, the 因为一切都成功,所以当控件到达PrintWriter is open when control reaches the finally block, which closes the PrintWriter.finally块时,PrintWriter将打开,从而关闭PrintWriter。Again, after the 同样,在finally block finishes executing, the program continues with the first statement after the finally block.finally块完成执行后,程序继续执行finally块后的第一条语句。
Here is the output from the 下面是ListOfNumbers program when no exceptions are thrown.ListOfNumber程序在没有引发异常时的输出。
Entering try statement Closing PrintWriter
The boldface code in the following sample shows the statements that get executed during this scenario.以下示例中的粗体代码显示了在此场景中执行的语句。
public void writeList() {
PrintWriter out = null;
try {
System.out.println("Entering try statement");
out = new PrintWriter(new FileWriter("OutFile.txt"));
for (int i = 0; i < SIZE; i++)
out.println("Value at: " + i + " = " + list.get(i));
} catch (IndexOutOfBoundsException e) {
System.err.println("Caught IndexOutOfBoundsException: "
+ e.getMessage());
} catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
} finally {
if (out != null) {
System.out.println("Closing PrintWriter");
out.close();
}
else {
System.out.println("PrintWriter not open");
}
}
}