checked_exception.md 7.7 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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
# 自定义可查异常(Checked Exception)

现有如下的学生考试成绩查询程序,会根据给定的字符串格式`studentID`进行学生分数查询,如果该学生存在,则返回其分数(确保为非负整数);如果该学生不存在,则返回`-1`,如下所示

```java
public class TestMain {
    private static boolean containsStudent(String studentID) {
        // Return true when the given `studentID` represents an existing student.
    }

    private static int queryScore(String studentID) {
        // Get and then return score of the student
        // whose ID is `StudentID` from the database.
    }

    private static int getScoreByStudentID(String studentID) {
        if (containsStudent(studentID)) {
            return queryScore(studentID);
        } else {
            return -1;
        }
    }

    public static void main(String[] args) {
        String studentID = "ID20211224";

        int score = getScoreByStudentID(studentID);
        if (score >= 0) {
            System.out.printf("Student ID : %s%n", studentID);
            System.out.printf("Score : %d%n", score);
        } else {
            System.out.printf("Student ID %s not found!%n", studentID);
        }
    }
}

```

请改写上述代码,对于学生不存在的情况,**使用可查异常(Checked Exception)对潜在的异常情况进行妥善处理**而不是返回`-1`

> 可查异常(Checked Exception)是编译器在编译时会进行检查,也称为编译时异常(Compile Time Exception)。 这些异常往往具备可预见性,且不能简单地被忽略,编程者应当对其进行妥善处理。

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

## 答案

```java
public class TestMain {
    private static boolean containsStudent(String studentID) {
        // Return true when the given `studentID` represents an existing student.
    }

    private static int queryScore(String studentID) {
        // Get and then return score of the student
        // whose ID is `StudentID` from the database.
    }

    public static class StudentNotExistException extends Exception {
        public StudentNotExistException(String studentID) {
            super(String.format("Student ID %s not found.", studentID));
        }
    }

    private static int getScoreByStudentID(String studentID) throws StudentNotExistException {
        if (containsStudent(studentID)) {
            return queryScore(studentID);
        } else {
            throw new StudentNotExistException(studentID);
        }
    }

    public static void main(String[] args) {
        String studentID = "ID20211224";

        try {
            int score = getScoreByStudentID(studentID);
            System.out.printf("Student ID : %s%n", studentID);
            System.out.printf("Score : %d%n", score);
        } catch (StudentNotExistException err) {
            System.out.printf("Student ID %s not found!%n", studentID);
        }
    }
}
```

## 选项

### A

```java
public class TestMain {
    private static boolean containsStudent(String studentID) {
        // Return true when the given `studentID` represents an existing student.
    }

    private static int queryScore(String studentID) {
        // Get and then return score of the student
        // whose ID is `StudentID` from the database.
    }

    public static class StudentNotExistException extends RuntimeException {
        public StudentNotExistException(String studentID) {
            super(String.format("Student ID %s not found.", studentID));
        }
    }

    private static int getScoreByStudentID(String studentID) throws StudentNotExistException {
        if (containsStudent(studentID)) {
            return queryScore(studentID);
        } else {
            throw new StudentNotExistException(studentID);
        }
    }

    public static void main(String[] args) {
        String studentID = "ID20211224";

        try {
            int score = getScoreByStudentID(studentID);
            System.out.printf("Student ID : %s%n", studentID);
            System.out.printf("Score : %d%n", score);
        } catch (StudentNotExistException err) {
            System.out.printf("Student ID %s not found!%n", studentID);
        }
    }
}
```

### B

```java
public class TestMain {
    private static boolean containsStudent(String studentID) {
        // Return true when the given `studentID` represents an existing student.
    }

    private static int queryScore(String studentID) {
        // Get and then return score of the student
        // whose ID is `StudentID` from the database.
    }

    public static class StudentNotExistException extends RuntimeException {
        public StudentNotExistException(String studentID) {
            super(String.format("Student ID %s not found.", studentID));
        }
    }

    private static int getScoreByStudentID(String studentID) {
        if (containsStudent(studentID)) {
            return queryScore(studentID);
        } else {
            throw new StudentNotExistException(studentID);
        }
    }

    public static void main(String[] args) {
        String studentID = "ID20211224";

        int score = getScoreByStudentID(studentID);
        System.out.printf("Student ID : %s%n", studentID);
        System.out.printf("Score : %d%n", score);
    }
}
```

### C

```java
public class TestMain {
    private static boolean containsStudent(String studentID) {
        // Return true when the given `studentID` represents an existing student.
    }

    private static int queryScore(String studentID) {
        // Get and then return score of the student
        // whose ID is `StudentID` from the database.
    }

    public static class StudentNotExistException extends Error {
        public StudentNotExistException(String studentID) {
            super(String.format("Student ID %s not found.", studentID));
        }
    }

    private static int getScoreByStudentID(String studentID) throws StudentNotExistException {
        if (containsStudent(studentID)) {
            return queryScore(studentID);
        } else {
            throw new StudentNotExistException(studentID);
        }
    }

    public static void main(String[] args) {
        String studentID = "ID20211224";

        try {
            int score = getScoreByStudentID(studentID);
            System.out.printf("Student ID : %s%n", studentID);
            System.out.printf("Score : %d%n", score);
        } catch (StudentNotExistException err) {
            System.out.printf("Student ID %s not found!%n", studentID);
        }
    }
}
```

### D

```java
public class TestMain {
    private static boolean containsStudent(String studentID) {
        // Return true when the given `studentID` represents an existing student.
    }

    private static int queryScore(String studentID) {
        // Get and then return score of the student
        // whose ID is `StudentID` from the database.
    }

    public static class StudentNotExistException extends Exception {
        public StudentNotExistException(String studentID) {
            super(String.format("Student ID %s not found.", studentID));
        }
    }

    private static int getScoreByStudentID(String studentID) throws StudentNotExistException {
        if (containsStudent(studentID)) {
            return queryScore(studentID);
        } else {
            throw new StudentNotExistException(studentID);
        }
    }

    public static void main(String[] args) {
        String studentID = "ID20211224";

        try {
            int score = getScoreByStudentID(studentID);
            System.out.printf("Student ID : %s%n", studentID);
            System.out.printf("Score : %d%n", score);
        } except (StudentNotExistException err) {
            System.out.printf("Student ID %s not found!%n", studentID);
        }
    }
}
```