Documentation

The Java™ Tutorials
Hide TOC
The catch Blockscatch块
Trail: Essential Java Classes
Lesson: Exceptions
Section: Catching and Handling Exceptions

The catch Blockscatch块

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.参数类型ExceptionType声明处理程序可以处理的异常类型,并且必须是从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.它们可以执行错误恢复、提示用户做出决定,或者使用链接异常将错误传播到更高级别的处理程序,如链式异常部分所述。

Catching More Than One Type of Exception with One Exception Handler使用一个异常处理程序捕获多个类型的异常

In Java SE 7 and later, a single catch block can handle more than one type of exception.在JavaSE7及更高版本中,单个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参数为隐式finalIn this example, the catch parameter ex is final and therefore you cannot assign any values to it within the catch block.在本例中,catch参数exfinal,因此不能在catch块中为其指定任何值。


Previous page: The try Block
Next page: The finally Block