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发行说明。
Valid Java programming language code must honor the Catch or Specify Requirement.有效的Java编程语言代码必须满足捕获或指定要求。This means that code that might throw certain exceptions must be enclosed by either of the following:这意味着可能引发某些异常的代码必须包含在以下任一项中:
try
statement that catches the exception.try
语句。try
must provide a handler for the exception, as described in Catching and Handling Exceptions.try
必须为异常提供处理程序,如捕获和处理异常中所述。throws
clause that lists the exception, as described in Specifying the Exceptions Thrown by a Method.throws
子句,如指定方法引发的异常中所述。Code that fails to honor the Catch or Specify Requirement will not compile.未能遵守捕获或指定要求的代码将不会编译。
Not all exceptions are subject to the Catch or Specify Requirement.并非所有异常情况都符合捕获或指定要求。To understand why, we need to look at the three basic categories of exceptions, only one of which is subject to the Requirement.为了理解原因,我们需要了解三种基本的异常情况,其中只有一种异常情况符合要求。
The first kind of exception is the checked exception.第一种异常是选中的异常。These are exceptional conditions that a well-written application should anticipate and recover from.这些都是编写良好的应用程序应该预料到并从中恢复的异常情况。For example, suppose an application prompts a user for an input file name, then opens the file by passing the name to the constructor for 例如,假设应用程序提示用户输入文件名,然后通过将名称传递给java.io.FileReader
.java.io.FileReader
的构造函数来打开该文件。Normally, the user provides the name of an existing, readable file, so the construction of the 通常,用户提供现有可读文件的名称,因此FileReader
object succeeds, and the execution of the application proceeds normally.FileReader
对象的构造成功,应用程序的执行正常进行。But sometimes the user supplies the name of a nonexistent file, and the constructor throws 但有时用户提供不存在的文件名,构造函数抛出java.io.FileNotFoundException
.java.io.FileNotFoundException
。A well-written program will catch this exception and notify the user of the mistake, possibly prompting for a corrected file name.编写良好的程序将捕获此异常并将错误通知用户,可能会提示输入正确的文件名。
Checked exceptions are subject to the Catch or Specify Requirement.已检查的异常情况受捕获或指定要求的约束。All exceptions are checked exceptions, except for those indicated by 所有异常都是检查异常,Error
, RuntimeException
, and their subclasses.Error
、RuntimeException
及其子类指示的异常除外。
The second kind of exception is the error.第二种异常是错误。These are exceptional conditions that are external to the application, and that the application usually cannot anticipate or recover from.这些是应用程序外部的异常情况,应用程序通常无法预料或从中恢复。For example, suppose that an application successfully opens a file for input, but is unable to read the file because of a hardware or system malfunction.例如,假设一个应用程序成功打开一个文件进行输入,但由于硬件或系统故障而无法读取该文件。The unsuccessful read will throw 读取失败将抛出java.io.IOError
.java.io.IOError
。An application might choose to catch this exception, in order to notify the user of the problem but it also might make sense for the program to print a stack trace and exit.应用程序可能会选择捕获此异常,以便通知用户问题但是,程序打印堆栈跟踪并退出也是有意义的。
Errors are not subject to the Catch or Specify Requirement.错误不受捕获或指定要求的约束。Errors are those exceptions indicated by 错误是由Error
and its subclasses.Error
及其子类指示的异常。
The third kind of exception is the runtime exception.第三种异常是运行时异常。These are exceptional conditions that are internal to the application, and that the application usually cannot anticipate or recover from.这些是应用程序内部的异常情况,应用程序通常无法预料或从中恢复。These usually indicate programming bugs, such as logic errors or improper use of an API.这些通常表示编程错误,例如逻辑错误或API使用不当。For example, consider the application described previously that passes a file name to the constructor for 例如,考虑前面描述的应用程序,该文件将文件名传递给FileReader
.FileReader
的构造函数。If a logic error causes a 如果逻辑错误导致将null
to be passed to the constructor, the constructor will throw NullPointerException
.null
传递给构造函数,则构造函数将抛出NullPointerException
。The application can catch this exception, but it probably makes more sense to eliminate the bug that caused the exception to occur.应用程序可以捕获此异常,但消除导致异常发生的错误可能更有意义。
Runtime exceptions are not subject to the Catch or Specify Requirement.运行时异常不受Catch或Specify要求的约束。Runtime exceptions are those indicated by 运行时异常是由RuntimeException
and its subclasses.RuntimeException
及其子类指示的异常。
Errors and runtime exceptions are collectively known as unchecked exceptions.错误和运行时异常统称为未检查异常。
Some programmers consider the Catch or Specify Requirement a serious flaw in the exception mechanism and bypass it by using unchecked exceptions in place of checked exceptions.有些程序员认为捕获或指定要求是异常机制中的严重缺陷,并通过使用未检查的异常代替检查异常来绕过它。In general, this is not recommended.一般来说,不建议这样做。The section Unchecked Exceptions The Controversy talks about when it is appropriate to use unchecked exceptions.未经检查的异常情况部分争论的焦点是何时应该使用未经检查的异常。