runtime_exception.md 5.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
# 自定义运行时异常(RuntimeException)

现有如下的海伦公式计算函数,根据三角形的三条边长`a``b``c`(均确保大于0)计算三角形的面积。当三角形本身非法时返回负数,否则返回面积,如下所示

```java
public class TestMain {
    private static float getArea(float a, float b, float c) {
        if ((a + b < c) || (a + c < b) || (b + c < a)) {
            return -1;  // 非法三角形
        } else {
            float p = (a + b + c) / 2;
            return (float) Math.sqrt((p - a) * (p - b) * (p - c) * p);  // 海伦公式
        }
    }

    public static void main(String[] args) {
        System.out.println(TestMain.getArea(3, 4, 5));
    }
}
```

请改写上述代码,对于三角形非法的情况,**仅在运行抛出不可查异常(Unchecked Exception)**而不是返回`-1`

> 不可查异常(Unchecked Exception)指在运行时发生的异常。这些也被称为运行时异常(Runtime Exception)。其中包括程序逻辑错误或API使用不当等,例如ArrayIndexOutOfBoundsException、IllegalStateException与NullPointerException等,此类异常将在编译时忽略,仅在运行时处理。

因此,下列修改方式正确的是:

## 答案

```java
public class TestMain {
    public static class InvalidTriangleException extends RuntimeException {
        public InvalidTriangleException(float a, float b, float c) {
            super(String.format("Invalid triangle - (%.3f, %.3f, %.3f).", a, b, c));
        }
    }

    private static float getArea(float a, float b, float c) {
        if ((a + b < c) || (a + c < b) || (b + c < a)) {
            throw new InvalidTriangleException(a, b, c);
        } else {
            float p = (a + b + c) / 2;
            return (float) Math.sqrt((p - a) * (p - b) * (p - c) * p);
        }
    }

    public static void main(String[] args) {
        System.out.println(TestMain.getArea(3, 4, 5));
    }
}
```

## 选项

### A

```java
public class TestMain {
    public class InvalidTriangleException extends RuntimeException {
        public InvalidTriangleException(float a, float b, float c) {
            super(String.format("Invalid triangle - (%.3f, %.3f, %.3f).", a, b, c));
        }
    }

    private float getArea(float a, float b, float c) {
        if ((a + b < c) || (a + c < b) || (b + c < a)) {
            throw new InvalidTriangleException(a, b, c);
        } else {
            float p = (a + b + c) / 2;
            return (float) Math.sqrt((p - a) * (p - b) * (p - c) * p);
        }
    }

    public static void main(String[] args) {
        System.out.println(TestMain.getArea(3, 4, 5));
    }
}
```

### B

```java
public class TestMain {
    public static class InvalidTriangleException extends Exception {
        public InvalidTriangleException(float a, float b, float c) {
            super(String.format("Invalid triangle - (%.3f, %.3f, %.3f).", a, b, c));
        }
    }

    private static float getArea(float a, float b, float c) {
        if ((a + b < c) || (a + c < b) || (b + c < a)) {
            throw new InvalidTriangleException(a, b, c);
        } else {
            float p = (a + b + c) / 2;
            return (float) Math.sqrt((p - a) * (p - b) * (p - c) * p);
        }
    }

    public static void main(String[] args) {
        System.out.println(TestMain.getArea(3, 4, 5));
    }
}
```

### C

```java
public class TestMain {
    public static class InvalidTriangleException extends Exception {
        public InvalidTriangleException(float a, float b, float c) {
            super(String.format("Invalid triangle - (%.3f, %.3f, %.3f).", a, b, c));
        }
    }

    private static float getArea(float a, float b, float c) throws InvalidTriangleException {
        if ((a + b < c) || (a + c < b) || (b + c < a)) {
            throw new InvalidTriangleException(a, b, c);
        } else {
            float p = (a + b + c) / 2;
            return (float) Math.sqrt((p - a) * (p - b) * (p - c) * p);
        }
    }

    public static void main(String[] args) throws InvalidTriangleException {
        System.out.println(TestMain.getArea(3, 4, 5));
    }
}
```

### D

```java
public class TestMain {
    public static class InvalidTriangleException extends Error {
        public InvalidTriangleException(float a, float b, float c) {
            super(String.format("Invalid triangle - (%.3f, %.3f, %.3f).", a, b, c));
        }
    }

    private static float getArea(float a, float b, float c) {
        if ((a + b < c) || (a + c < b) || (b + c < a)) {
            throw new InvalidTriangleException(a, b, c);
        } else {
            float p = (a + b + c) / 2;
            return (float) Math.sqrt((p - a) * (p - b) * (p - c) * p);
        }
    }

    public static void main(String[] args) {
        System.out.println(TestMain.getArea(3, 4, 5));
    }
}
```

### E

```java
public class TestMain {
    public static class InvalidTriangleException extends RuntimeException {
        public InvalidTriangleException(float a, float b, float c) {
            super(String.format("Invalid triangle - (%.3f, %.3f, %.3f).", a, b, c));
        }
    }

    private static float getArea(float a, float b, float c) throws InvalidTriangleException {
        if ((a + b < c) || (a + c < b) || (b + c < a)) {
            throw new InvalidTriangleException(a, b, c);
        } else {
            float p = (a + b + c) / 2;
            return (float) Math.sqrt((p - a) * (p - b) * (p - c) * p);
        }
    }

    public static void main(String[] args) throws InvalidTriangleException {
        System.out.println(TestMain.getArea(3, 4, 5));
    }
}
```