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发行说明。
Before you can catch an exception, some code somewhere must throw one.在捕获异常之前,某个地方的某些代码必须抛出一个异常。Any code can throw an exception: your code, code from a package written by someone else such as the packages that come with the Java platform, or the Java runtime environment.任何代码都可能引发异常:您的代码、来自其他人编写的包(如Java平台附带的包)或Java运行时环境的代码。Regardless of what throws the exception, it's always thrown with the 不管抛出异常的是什么,它总是与throw
statement.throw
语句一起抛出。
As you have probably noticed, the Java platform provides numerous exception classes.您可能已经注意到,Java平台提供了许多异常类。All the classes are descendants of the 所有类都是Throwable
class, and all allow programs to differentiate among the various types of exceptions that can occur during the execution of a program.Throwable
类的后代,并且所有类都允许程序区分在程序执行期间可能发生的各种类型的异常。
You can also create your own exception classes to represent problems that can occur within the classes you write.您还可以创建自己的异常类来表示您编写的类中可能出现的问题。In fact, if you are a package developer, you might have to create your own set of exception classes to allow users to differentiate an error that can occur in your package from errors that occur in the Java platform or other packages.事实上,如果您是包开发人员,您可能必须创建自己的异常类集,以允许用户区分包中可能发生的错误与Java平台或其他包中发生的错误。
You can also create chained exceptions.您还可以创建链接异常。For more information, see the Chained Exceptions section.有关更多信息,请参阅链式异常部分。
All methods use the 所有方法都使用throw
statement to throw an exception.throw
语句来抛出异常。The throw
statement requires a single argument: a throwable object.throw
语句需要一个参数:可抛出对象。Throwable objects are instances of any subclass of the 可抛出对象是Throwable
class.Throwable
类的任何子类的实例。Here's an example of a 下面是一个throw
statement.throw
语句的示例。
throw someThrowableObject;
Let's look at the 让我们在上下文中看一下throw
statement in context.throw
语句。The following 以下pop
method is taken from a class that implements a common stack object.pop
方法取自实现公共堆栈对象的类。The method removes the top element from the stack and returns the object.该方法从堆栈中移除顶层元素并返回对象。
public Object pop() { Object obj; if (size == 0) { throw new EmptyStackException(); } obj = objectAt(size - 1); setObjectAt(size - 1, null); size--; return obj; }
The pop
method checks to see whether any elements are on the stack.pop
方法检查堆栈上是否有任何元素。If the stack is empty (its size is equal to 如果堆栈为空(其大小等于0
), pop
instantiates a new EmptyStackException
object (a member of java.util
) and throws it.0
),pop
将实例化一个新的EmptyStackException
对象(java.util
的成员)并抛出它。The Creating Exception Classes section in this chapter explains how to create your own exception classes.本章中的创建异常类部分解释了如何创建自己的异常类。For now, all you need to remember is that you can throw only objects that inherit from the 现在,您需要记住的是,您只能抛出从java.lang.Throwable
class.java.lang.Throwable
类继承的对象。
Note that the declaration of the 请注意,pop
method does not contain a throws
clause.pop
方法的声明不包含throws
子句。EmptyStackException
is not a checked exception, so pop
is not required to state that it might occur.EmptyStackException
不是选中的异常,因此不需要pop
来声明它可能发生。
The objects that inherit from the 从Throwable
class include direct descendants (objects that inherit directly from the Throwable
class) and indirect descendants (objects that inherit from children or grandchildren of the Throwable
class).Throwable
类继承的对象包括直接子对象(直接从Throwable
类继承的对象)和间接子对象(从Throwable
类的子对象或孙子对象继承的对象)。The figure below illustrates the class hierarchy of the 下图说明了Throwable
class and its most significant subclasses.Throwable
类及其最重要的子类的类层次结构。As you can see, 正如您所看到的,Throwable
has two direct descendants: Error
and Exception
.Throwable
有两个直接的后代:Error
和Exception
。
The Throwable class.
When a dynamic linking failure or other hard failure in the Java virtual machine occurs, the virtual machine throws an 当Java虚拟机中发生动态链接故障或其他硬故障时,虚拟机将抛出一个错误。Error
.Simple programs typically do not catch or throw 简单程序通常不会捕获或抛出错误。Error
s.
Most programs throw and catch objects that derive from the 大多数程序抛出并捕获从Exception
class.Exception
类派生的对象。An Exception
indicates that a problem occurred, but it is not a serious system problem.Exception
表示出现了问题,但不是严重的系统问题。Most programs you write will throw and catch 您编写的大多数程序都会抛出并捕获Exception
s as opposed to Error
s.Exception
,而不是Error
。
The Java platform defines the many descendants of the Java平台定义了Exception
class.Exception
类的许多子类。These descendants indicate various types of exceptions that can occur.这些子体表示可能发生的各种类型的异常。For example, 例如,IllegalAccessException
signals that a particular method could not be found, and NegativeArraySizeException
indicates that a program attempted to create an array with a negative size.IllegalAccessException
表示找不到特定方法,NegativeArraySizeException
表示程序试图创建大小为负的数组。
One 一个Exception
subclass, RuntimeException
, is reserved for exceptions that indicate incorrect use of an API.Exception
子类RuntimeException
是为指示API使用不正确的异常保留的。An example of a runtime exception is 运行时异常的一个示例是NullPointerException
, which occurs when a method tries to access a member of an object through a null
reference.NullPointerException
,当方法尝试通过null
引用访问对象的成员时会发生此异常。The section Unchecked Exceptions The Controversy discusses why most applications shouldn't throw runtime exceptions or subclass 未经检查的异常争论部分讨论了为什么大多数应用程序不应该抛出运行时异常或子类RuntimeException
.RuntimeException
。