mirror of
https://gitee.com/gaohongy/Java-Book.git
synced 2026-07-16 10:33:49 +00:00
增加自定义异常的例子
This commit is contained in:
+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