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发行说明。
You associate exception handlers with a 通过在try
block by providing one or more catch
blocks directly after the try
block.try
块之后直接提供一个或多个catch
块,可以将异常处理程序与try
块相关联。No code can be between the end of the try
block and the beginning of the first catch
block.try
块的结尾和第一个catch
块的开头之间不能有代码。
try { } catch (ExceptionType name) { } catch (ExceptionType name) { }
Each 每个catch
block is an exception handler that handles the type of exception indicated by its argument.catch
块都是一个异常处理程序,用于处理由其参数指示的异常类型。The argument type, 参数类型ExceptionType
, declares the type of exception that the handler can handle and must be the name of a class that inherits from the Throwable
class.ExceptionTyp
e声明处理程序可以处理的异常类型,并且必须是从Throwable
类继承的类的名称。The handler can refer to the exception with 处理程序可以使用name
.name
引用异常。
The catch
block contains code that is executed if and when the exception handler is invoked.catch
块包含在调用异常处理程序时执行的代码。The runtime system invokes the exception handler when the handler is the first one in the call stack whose 当异常处理程序是调用堆栈中ExceptionType
matches the type of the exception thrown.ExceptionType
与引发的异常类型匹配的第一个异常处理程序时,运行时系统调用该异常处理程序。The system considers it a match if the thrown object can legally be assigned to the exception handler's argument.如果抛出的对象可以合法地分配给异常处理程序的参数,则系统将其视为匹配。
The following are two exception handlers for the 以下是writeList
method:writeList
方法的两个异常处理程序:
try { } catch (IndexOutOfBoundsException e) { System.err.println("IndexOutOfBoundsException: " + e.getMessage()); } catch (IOException e) { System.err.println("Caught IOException: " + e.getMessage()); }
Exception handlers can do more than just print error messages or halt the program.异常处理程序可以做的不仅仅是打印错误消息或停止程序。They can do error recovery, prompt the user to make a decision, or propagate the error up to a higher-level handler using chained exceptions, as described in the Chained Exceptions section.它们可以执行错误恢复、提示用户做出决定,或者使用链接异常将错误传播到更高级别的处理程序,如链式异常部分所述。
In Java SE 7 and later, a single 在JavaSE7及更高版本中,单个catch
block can handle more than one type of exception.catch
块可以处理多种类型的异常。This feature can reduce code duplication and lessen the temptation to catch an overly broad exception.此功能可以减少代码重复,并减少捕获过度广泛异常的诱惑。
In the 在catch
clause, specify the types of exceptions that block can handle, and separate each exception type with a vertical bar (|
):catch
子句中,指定块可以处理的异常类型,并用竖线(|
)分隔每个异常类型:
catch (IOException|SQLException ex) { logger.log(ex); throw ex; }
Note: If a 注意:如果catch
block handles more than one exception type, then the catch
parameter is implicitly final
.catch
块处理多个异常类型,则catch
参数为隐式final
。In this example, the 在本例中,catch
parameter ex
is final
and therefore you cannot assign any values to it within the catch
block.catch
参数ex
是final
,因此不能在catch
块中为其指定任何值。