java中的异常--Exceptions in Java-- 第一部分

简介: 作者和出处     By Bill Venners, JavaWorld.com  http://www.javaworld.com/javaworld/jw-07-1998/jw-07-exceptions.html Exceptions in Java     当一个方法遇到了一个他不能处理的异常,这个方法有可能抛出异常。

作者和出处     By Bill Venners, JavaWorld.com 

http://www.javaworld.com/javaworld/jw-07-1998/jw-07-exceptions.html

Exceptions in Java

    当一个方法遇到了一个他不能处理的异常,这个方法有可能抛出异常。抛出一个异常就像抛出一个(震动、闪烁的红)闪光的球,去指出发生不能被处理问题的位置。不管在什么地方 ,你都希望这个异常被捕获并得到处理。异常被捕获处理的位置伴随线程的堆栈。如果调用方法没有准备捕获这个异常,这个方法将会把这个异常抛出到调用这个方法的方法中去,如果还没有将继续抛出这个异常到更上级调用方法中去。如果程序中一个线程抛出一个不能被其堆栈中的任何方法捕获的异常,这个线程将被终止。当你使用JAVA变成的时候,你必须指出异常处理 策略,所以你的程序需要将捕获并处理所有你想要恢复的异常。

      When a method encounters an abnormal condition (an exception condition) that it can't handle itself, it may throw an exception. Throwing an exception is like throwing a beeping, flashing red ball to indicate there is a problem that can't be handled where it occurred. Somewhere, you hope, this ball will be caught and the problem will be dealt with. Exceptions are caught by handlers positioned along the thread's method invocation stack. If the calling method isn't prepared to catch the exception, it throws the exception up to its calling method, and so on. If one of the threads of your program throws an exception that isn't caught by any method along the method invocation stack, that thread will expire. When you program in Java, you must position catchers (the exception handlers) strategically, so your program will catch and handle all exceptions from which you want your program to recover.

 

Exception classes      异常类

在JAVA中,异常都是对象。当你抛出一个异常,你就抛出了一个对象。你不能把所有对象都作为一个异常抛出,但是--这些对象都是Throwable的子类。在java.lang声明中,Throwable作为一个程序能够初始化和抛出异常处理结构的基类。一个小的异常类图结构被展示在图Figure 1中。

In Java, exceptions are objects. When you throw an exception, you throw an object. You can't throw just any object as an exception, however -- only those objects whose classes descend from Throwable. Throwable serves as the base class for an entire family of classes, declared in java.lang, that your program can instantiate and throw. A small part of this family is shown in Figure 1.

 

Figure 1. A partial view of the Throwable family 

除了抛出在java.lang中定义的这些对象,你能够抛出你自己设计的对象。创建自己的throwable对象,你仅仅需要声明对象为throwable family中的一员。一般来说throwable类是你扩展Exception类而来的。他们应该是exceptions。这个规则的原因之后将被解释。

In addition to throwing objects whose classes are declared in java.lang, you can throw objects of your own design. To create your own class of throwable objects, you need only declare it as a subclass of some member of the Throwable family. In general, however, the throwable classes you define should extend class Exception. They should be "exceptions." The reasoning behind this rule will be explained later in this article.

根据实际情况使用一个存在于java.lang中的或者自己创建的异常。在某些情况下,一个从java.lang集成而来的类将是更好的选择。例如,如果你的一个方法被传入一个无效参数调用,你可以能抛出无效参数异常(IllegalArgumentException在java.lang中RuntimeException 的子类)。

Whether you use an existing exception class from java.lang or create one of your own depends upon the situation. In some cases, a class from java.lang will do just fine. For example, if one of your methods is invoked with an invalid argument, you could throw IllegalArgumentException, a subclass of RuntimeException in java.lang.  

目录
相关文章
|
2月前
|
Java
java自定义Service的异常
java自定义Service的异常
15 0
|
2月前
|
Java
Java中的异常链:从根源到解决方案
Java中的异常链:从根源到解决方案
41 0
|
2月前
|
存储 监控 Java
Java认识异常(超级详细)
Java认识异常(超级详细)
|
9天前
|
Java 程序员 编译器
|
10天前
Swagger2异常:java.lang.NumberFormatException: For input string: ““
Swagger2异常:java.lang.NumberFormatException: For input string: ““
17 1
|
12天前
|
存储 Java 程序员
JavaSE&Java的异常
JavaSE&Java的异常
23 0
|
23天前
|
Java 开发者
Java中的受检异常和非受检异常的区别
Java中的受检异常和非受检异常的区别
|
28天前
|
Java
Java中的异常类总结
Java中的异常类总结
|
2月前
|
SQL Java
java中的异常
java中的异常
10 1
|
2月前
|
Java 程序员 编译器
Java中异常
Java中异常
12 0