JAVA异常资料翻译(三)

原文链接:
原文:
How to Throw Exceptions
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. Regardless of what throws the exception, it’s always thrown with the throw statement.

As you have probably noticed, the Java platform provides numerous exception classes. 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.

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.

You can also create chained exceptions. For more information, see the Chained Exceptions section.

The throw Statement
All methods use the throw statement to throw an exception. The throw statement requires a single argument: a throwable object. Throwable objects are instances of any subclass of the Throwable class. Here’s an example of a throw statement.

throw someThrowableObject;
Let’s look at the throw statement in context. The following pop method is taken from a class that implements a common stack object. 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. 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. 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.

Note that the declaration of the pop method does not contain a throws clause. EmptyStackException is not a checked exception, so pop is not required to state that it might occur.

Throwable Class and Its Subclasses
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). The figure below illustrates the class hierarchy of the Throwable class and its most significant subclasses. As you can see, Throwable has two direct descendants: Error and Exception.

The Throwable class and its most significant subclasses.
The Throwable class.

Error Class
When a dynamic linking failure or other hard failure in the Java virtual machine occurs, the virtual machine throws an Error. Simple programs typically do not catch or throw Errors.

Exception Class
Most programs throw and catch objects that derive from the Exception class. An Exception indicates that a problem occurred, but it is not a serious system problem. Most programs you write will throw and catch Exceptions as opposed to Errors.

The Java platform defines the many descendants of the Exception class. 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.

One Exception subclass, RuntimeException, is reserved for exceptions that indicate incorrect use of an 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. The section Unchecked Exceptions — The Controversy discusses why most applications shouldn’t throw runtime exceptions or subclass RuntimeException.
译文:

如何抛出异常

在你捕捉一个异常之前,某个地方的一块代码必须抛出异常。任何代码都可能抛出异常:你的代码,来自其他人编写包的代码,比如来自java平台的包,或是java运行环境。无论抛出什么异常,一定会使用throw语句抛出异常。
你可能注意过,java平台提供了很多异常类。所有的异常类都是throwable类的子类,而且都能让一个程序在执行时区分不同类型的异常。
你也能创建一个你自己的异常类来呈现你写的类可能出现的问题。实际上,如果你是一个包的开发者,你一定会创建你自己的异常类,来让用户去区分在用你的写的包和来自java平台的其他包时碰到的错误。
你也能创建链式异常。想看到更多信息,可以去浏览链式集合这节课程。

throw语句

所有的方法都会使用throw语句去抛出异常。throw语句的要求是单一的变量:一个可以抛出的对象。可以抛出的对象是throwable类的任何子类的实例。下面是一个thorw语句的例子。
pop方法检查堆栈里是是否有元素。如果堆栈是空的(他的容量接近0),pop方法就会实例化一个新的EmptyStackException 类(是java.util的成员)并且抛出它。创建异常类的课程这节课程中,解释了如何去创建一个你自己的异常类。现在,你需要记住的,是你只能抛出从java.lang.Throwable类中继承的子类。
注意,pop方法的声明不包含throws语句。EmptyStackException不是一个受检异常,所以pop方法不能声明它可能会发生。

可抛出类和他的子类

继承throwable的类包括了他直接的子代(直接继承throwable的类)以及间接继承的子代(继承throwable类的子类或是子孙类)。下图说明了throwable类和他最重要的子类结构。正如你看到的,throwable类有两个直接的子类:错误和异常。

错误类

当动态链接失败或是其他存在于Java虚拟机的硬故障发生时,虚拟机会抛出一个错误。简单的程序通常不会捕获或者抛出错误。

异常类

大多数程序抛出或者捕获从exception类派生的对象。一个异常表明出现了一个问题,但是它对于系统不是一个很严重的问题。大多数你写的程序可能会抛出并捕获异常而不是错误。
Java平台定义了很多的异常类的子类。这些子类意味着很多类型的异常会发生。举个例子,IllegalAccessException 说明一个特别的方法不能被找到,而NegativeArraySizeException 表明一个程序尝试创建一个容量为负值的数组。
一个异常的子类,RuntimeException,是用来表明不正确使用一个API的。运行时异常的一个例子就是NullPointerException,发生在当一个方法尝试通过空的参数去链接一个对象的成员。这在“非受检异常——争论”这节课程中会讨论为什么大多数应用程序不抛出运行时异常或者子类的运行时异常。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值