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发行说明。
This section describes how to use the three exception handler components the 本节介绍如何使用三个异常处理程序组件try
, catch
, and finally
blocks to write an exception handler.try
、catch
和finally
块编写异常处理程序。Then, the 然后,解释了Java SE 7中引入的try-
with-resources statement, introduced in Java SE 7, is explained.try-
with-resources语句。The try-
with-resources statement is particularly suited to situations that use Closeable
resources, such as streams.try-
with-resources语句特别适合于使用可关闭资源(如流)的情况。
The last part of this section walks through an example and analyzes what occurs during various scenarios.本节的最后一部分将介绍一个示例,并分析在各种场景中发生的情况。
The following example defines and implements a class named 下面的示例定义并实现了一个名为ListOfNumbers
.ListOfNumber
的类。When constructed, 构造时,ListOfNumbers
creates an ArrayList
that contains 10 Integer
elements with sequential values 0 through 9.ListOfNumbers
会创建一个ArrayList
,其中包含10个连续值为0到9的整数元素。The ListOfNumbers
class also defines a method named writeList
, which writes the list of numbers into a text file called OutFile.txt
.ListOfNumbers
类还定义了一个名为writeList
的方法,该方法将数字列表写入名为OutFile.txt
的文本文件中。This example uses output classes defined in 本例使用java.io
, which are covered in Basic I/O.java.io
中定义的输出类,这些类在基本输入输出中有介绍。
// Note: This class will not compile yet. import java.io.*; import java.util.List; import java.util.ArrayList; public class ListOfNumbers { private List<Integer> list; private static final int SIZE = 10; public ListOfNumbers () { list = new ArrayList<Integer>(SIZE); for (int i = 0; i < SIZE; i++) { list.add(new Integer(i)); } } public void writeList() { // The FileWriter constructor throws IOException, which must be caught. PrintWriter out = new PrintWriter(new FileWriter("OutFile.txt")); for (int i = 0; i < SIZE; i++) { // The get(int) method throws IndexOutOfBoundsException, which must be caught. out.println("Value at: " + i + " = " + list.get(i)); } out.close(); } }
The first line in boldface is a call to a constructor.粗体的第一行是对构造函数的调用。The constructor initializes an output stream on a file.构造函数初始化文件上的输出流。If the file cannot be opened, the constructor throws an 如果无法打开文件,构造函数将抛出IOException
.IOException
。The second boldface line is a call to the 第二条粗体线是对ArrayList
class's get
method, which throws an IndexOutOfBoundsException
if the value of its argument is too small (less than 0) or too large (more than the number of elements currently contained by the ArrayList
).ArrayList
类的get
方法的调用,如果其参数值太小(小于0)或太大(大于ArrayList
当前包含的元素数),则会引发IndexOutOfBoundsException
。
If you try to compile the 如果尝试编译ListOfNumbers
class, the compiler prints an error message about the exception thrown by the FileWriter
constructor.ListOfNumbers
类,编译器将打印一条关于FileWriter
构造函数引发的异常的错误消息。However, it does not display an error message about the exception thrown by 但是,它不会显示有关get
.get
引发的异常的错误消息。The reason is that the exception thrown by the constructor, 原因是构造函数IOException
, is a checked exception, and the one thrown by the get
method, IndexOutOfBoundsException
, is an unchecked exception.IOException
引发的异常是已检查的异常,而get
方法IndexOutOfBoundsException
引发的异常是未检查的异常。
Now that you're familiar with the 现在,您已经熟悉了ListOfNumbers
class and where the exceptions can be thrown within it, you're ready to write exception handlers to catch and handle those exceptions.ListOfNumbers
类以及可以在其中抛出异常的位置,现在可以编写异常处理程序来捕获和处理这些异常了。