增加自定义异常的例子

master
高宏宇 1 month ago
parent cacaa718d3
commit 573873666f

@ -19,7 +19,7 @@
如果大家都清楚上述的规则当我们讨论猫种的时候就默认猫种具备祖先的所有特性而不必再说明猫种是脊椎动物胎生等这些它祖先所具备的特性。这种特性在Java种的类种也存在这就是继承。 如果大家都清楚上述的规则当我们讨论猫种的时候就默认猫种具备祖先的所有特性而不必再说明猫种是脊椎动物胎生等这些它祖先所具备的特性。这种特性在Java种的类种也存在这就是继承。
### 1.2. Java中的继承 ### 1.2. Java中的继承(411)
例如要在平面上描述两个类圆和矩形那么这两个类好像有一些基本的特性是一致的。例如填充颜色color是否填充 filled对象创建时间 dateCreated等。另外需要对上述属性设置或者读取的方法getColor()setColor()isFilled()setFilled()等。 例如要在平面上描述两个类圆和矩形那么这两个类好像有一些基本的特性是一致的。例如填充颜色color是否填充 filled对象创建时间 dateCreated等。另外需要对上述属性设置或者读取的方法getColor()setColor()isFilled()setFilled()等。

@ -440,7 +440,95 @@ public class Test {
## 4. 自定义异常 ## 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;
}
}
```
![image-20260410132946665](/run/media/danny/Data/CUIT/Java-Book/04异常处理/img/image-20260410132946665.png)
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. 本章重点 ## 5. 本章重点

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Loading…
Cancel
Save