diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/checked_exception.json" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/checked_exception.json" new file mode 100644 index 0000000000000000000000000000000000000000..c4d2103ca31f4d8939d10d05c70d9f3fb2b539de --- /dev/null +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/checked_exception.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "author": "HansBug", + "source": "checked_exception.md", + "notebook_enable": false, + "exercise_id": "302ed00decc24b4ba435aab028582ac4" +} \ No newline at end of file diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/checked_exception.md" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/checked_exception.md" new file mode 100644 index 0000000000000000000000000000000000000000..e4f19115136961fa29bd003649367d0d6c8629a5 --- /dev/null +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/checked_exception.md" @@ -0,0 +1,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); + } + } +} +``` + diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/config.json" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/config.json" index 2db98dc8bbf2f51686e525054811a3c7a8ff042a..53a7e14d9a8b75466aa1bce95aa031ec418809aa 100644 --- "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/config.json" +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/config.json" @@ -223,7 +223,9 @@ ], "export": [ "exception.json", - "using.json" + "using.json", + "runtime_exception.json", + "checked_exception.json" ], "title": "通过异常处ç†é”™è¯¯" } \ No newline at end of file diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/runtime_exception.json" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/runtime_exception.json" new file mode 100644 index 0000000000000000000000000000000000000000..4e507423825622e686a383e5e11e09876f840f6a --- /dev/null +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/runtime_exception.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "author": "HansBug", + "source": "runtime_exception.md", + "notebook_enable": false, + "exercise_id": "61a61ce93dca4b2e9049f5318e4d2a21" +} \ No newline at end of file diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/runtime_exception.md" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/runtime_exception.md" new file mode 100644 index 0000000000000000000000000000000000000000..c48916024540732212d4b32337d29b5742f0947b --- /dev/null +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/runtime_exception.md" @@ -0,0 +1,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)); + } +} +``` +