diff --git a/03:类与对象/03.md b/03:类与对象/03.md index 5b43d80..12523f1 100644 --- a/03:类与对象/03.md +++ b/03:类与对象/03.md @@ -19,7 +19,7 @@ 如果大家都清楚上述的规则,当我们讨论猫种的时候,就默认猫种具备祖先的所有特性,而不必再说明猫种是脊椎动物,胎生等这些它祖先所具备的特性。这种特性在Java种的类种也存在,这就是继承。 -### 1.2. Java中的继承 +### 1.2. Java中的继承(411) 例如要在平面上描述两个类,圆和矩形,那么这两个类好像有一些基本的特性是一致的。例如,填充颜色color,是否填充 filled,对象创建时间 dateCreated等。另外,需要对上述属性设置或者读取的方法:getColor(),setColor(),isFilled(),setFilled()等。 diff --git a/04:异常处理/01.md b/04:异常处理/01.md index 06f4e7f..de2f6af 100644 --- a/04:异常处理/01.md +++ b/04:异常处理/01.md @@ -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; + } +} +``` + +![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. 本章重点 diff --git a/04:异常处理/img/image-20260410132946665.png b/04:异常处理/img/image-20260410132946665.png new file mode 100644 index 0000000..5c617cc Binary files /dev/null and b/04:异常处理/img/image-20260410132946665.png differ