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发行说明。
A program can use exceptions to indicate that an error occurred.程序可以使用异常来指示发生了错误。To throw an exception, use the 要抛出异常,请使用throw statement and provide it with an exception object a descendant of Throwable to provide information about the specific error that occurred.throw语句并为其提供异常对象Throwable的后代提供有关发生的特定错误的信息。A method that throws an uncaught, checked exception must include a 抛出未捕获、已检查异常的方法必须在其声明中包含throws clause in its declaration.throws子句。
A program can catch exceptions by using a combination of the 程序可以通过使用try, catch, and finally blocks.try、catch和finally块的组合来捕获异常。
try block identifies a block of code in which an exception can occur.try块标识可能发生异常的代码块。catch block identifies a block of code, known as an exception handler, that can handle a particular type of exception.catch块标识一个代码块,称为异常处理程序,可以处理特定类型的异常。finally block identifies a block of code that is guaranteed to execute, and is the right place to close files, recover resources, and otherwise clean up after the code enclosed in the try block.finally块标识保证执行的代码块,是关闭文件、恢复资源以及在try块中包含的代码之后进行清理的正确位置。The try statement should contain at least one catch block or a finally block and may have multiple catch blocks.try语句应至少包含一个catch块或finally块,并且可能有多个catch块。
The class of the exception object indicates the type of exception thrown.异常对象的类指示引发的异常的类型。The exception object can contain further information about the error, including an error message.异常对象可以包含有关错误的进一步信息,包括错误消息。With exception chaining, an exception can point to the exception that caused it, which can in turn point to the exception that caused it, and so on.通过异常连缀,异常可以指向导致它的异常,而异常又可以指向导致它的异常,依此类推。