mirror of
https://gitee.com/gaohongy/Java-Book.git
synced 2026-07-16 09:03:49 +00:00
增加自定义异常的例子
This commit is contained in:
+1
-1
@@ -19,7 +19,7 @@
|
||||
|
||||
如果大家都清楚上述的规则,当我们讨论猫种的时候,就默认猫种具备祖先的所有特性,而不必再说明猫种是脊椎动物,胎生等这些它祖先所具备的特性。这种特性在Java种的类种也存在,这就是继承。
|
||||
|
||||
### 1.2. Java中的继承
|
||||
### 1.2. Java中的继承(411)
|
||||
|
||||
例如要在平面上描述两个类,圆和矩形,那么这两个类好像有一些基本的特性是一致的。例如,填充颜色color,是否填充 filled,对象创建时间 dateCreated等。另外,需要对上述属性设置或者读取的方法:getColor(),setColor(),isFilled(),setFilled()等。
|
||||
|
||||
|
||||
+89
-1
@@ -440,7 +440,95 @@ public class Test {
|
||||
|
||||
## 4. 自定义异常
|
||||
|
||||
略,留给大家自己去探索。
|
||||
> Java provides quite a few exception classes. Use them whenever possible instead of defining your own exception classes. However, if you run into a problem that cannot be adequately described by the predefined exception classes, you can create your own exception class, derived from Exception or from a subclass of Exception, such as IOException.
|
||||
>
|
||||
> In Listing 12.7, CircleWithException.java, the setRadius method throws an exception if the radius is negative. Suppose you wish to pass the radius to the handler. In that case, you can define a custom exception class, as shown in Listing 12.10.
|
||||
|
||||
InvalidRadiusException.java
|
||||
|
||||
```java
|
||||
public class InvalidRadiusException extends Exception {
|
||||
private double radius;
|
||||
|
||||
/** Construct an exception */
|
||||
public InvalidRadiusException(double radius) {
|
||||
super("Invalid radius " + radius);
|
||||
this.radius = radius;
|
||||
}
|
||||
|
||||
/** Return the radius */
|
||||
public double getRadius() {
|
||||
return radius;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||

|
||||
|
||||
TestCircleWithCustomException.java
|
||||
|
||||
```java
|
||||
public class TestCircleWithCustomException {
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
new CircleWithCustomException(5);
|
||||
new CircleWithCustomException(-5);
|
||||
new CircleWithCustomException(0);
|
||||
} catch (InvalidRadiusException ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
|
||||
System.out.println("Number of objects created: " + CircleWithCustomException.getNumberOfObjects());
|
||||
}
|
||||
}
|
||||
|
||||
class CircleWithCustomException {
|
||||
/** The radius of the circle */
|
||||
private double radius;
|
||||
|
||||
/** The number of the objects created */
|
||||
private static int numberOfObjects = 0;
|
||||
|
||||
/** Construct a circle with radius 1 */
|
||||
public CircleWithCustomException() throws InvalidRadiusException {
|
||||
this(1.0);
|
||||
}
|
||||
|
||||
/** Construct a circle with a specified radius */
|
||||
public CircleWithCustomException(double newRadius) throws InvalidRadiusException {
|
||||
setRadius(newRadius);
|
||||
numberOfObjects++;
|
||||
}
|
||||
|
||||
/** Return radius */
|
||||
public double getRadius() {
|
||||
return radius;
|
||||
}
|
||||
|
||||
/** Set a new radius */
|
||||
public void setRadius(double newRadius) throws InvalidRadiusException {
|
||||
if (newRadius >= 0)
|
||||
radius = newRadius;
|
||||
else
|
||||
throw new InvalidRadiusException(newRadius);
|
||||
}
|
||||
|
||||
/** Return numberOfObjects */
|
||||
public static int getNumberOfObjects() {
|
||||
return numberOfObjects;
|
||||
}
|
||||
|
||||
/** Return the area of this circle */
|
||||
public double findArea() {
|
||||
return radius * radius * 3.14159;
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## 5. 本章重点
|
||||
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 50 KiB |
Reference in New Issue
Block a user