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发行说明。
An application often responds to an exception by throwing another exception.应用程序通常通过抛出另一个异常来响应异常。In effect, the first exception causes the second exception.实际上,第一个异常会导致第二个异常。It can be very helpful to know when one exception causes another.了解一个异常何时导致另一个异常非常有帮助。Chained Exceptions help the programmer do this.链式异常有助于程序员做到这一点。
The following are the methods and constructors in 下面是Throwable that support chained exceptions.Throwable中支持链式异常的方法和构造函数。
Throwable getCause() Throwable initCause(Throwable) Throwable(String, Throwable) Throwable(Throwable)
The Throwable argument to initCause and the Throwable constructors is the exception that caused the current exception.initCause和Throwable构造函数的Throwable参数是导致当前异常的异常。getCause returns the exception that caused the current exception, and initCause sets the current exception's cause.getCause返回导致当前异常的异常,initCause设置当前异常的原因。
The following example shows how to use a chained exception.下面的示例演示如何使用链接异常。
try {
} catch (IOException e) {
throw new SampleException("Other IOException", e);
}In this example, when an 在本例中,当捕获IOException is caught, a new SampleException exception is created with the original cause attached and the chain of exceptions is thrown up to the next higher level exception handler.IOException时,将创建一个新的SampleException异常,并附加原始原因,然后将异常链抛出到下一个更高级别的异常处理程序。
Now let's suppose that the higher-level exception handler wants to dump the stack trace in its own format.现在让我们假设高级异常处理程序希望以自己的格式转储堆栈跟踪。
The following code shows how to call the 下面的代码显示了如何在getStackTrace method on the exception object.exception对象上调用getStackTrace方法。
catch (Exception cause) {
StackTraceElement elements[] = cause.getStackTrace();
for (int i = 0, n = elements.length; i < n; i++) {
System.err.println(elements[i].getFileName()
+ ":" + elements[i].getLineNumber()
+ ">> "
+ elements[i].getMethodName() + "()");
}
}The next code snippet logs where an exception occurred from within the 下一个代码段记录catch block.catch块中发生异常的位置。However, rather than manually parsing the stack trace and sending the output to 但是,它不是手动解析堆栈跟踪并将输出发送到System.err(), it sends the output to a file using the logging facility in the java.util.logging package.System.err(),而是使用java.util.logging包中的日志功能将输出发送到文件。
try {
Handler handler = new FileHandler("OutFile.log");
Logger.getLogger("").addHandler(handler);
} catch (IOException e) {
Logger logger = Logger.getLogger("package.name");
StackTraceElement elements[] = e.getStackTrace();
for (int i = 0, n = elements.length; i < n; i++) {
logger.log(Level.WARNING, elements[i].getMethodName());
}
}