{"id":77459,"date":"2026-02-24T18:43:26","date_gmt":"2026-02-24T10:43:26","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/77459.html"},"modified":"2026-02-24T18:43:26","modified_gmt":"2026-02-24T10:43:26","slug":"15-java%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e5%85%a8%e8%a7%a3%e6%9e%90%ef%bc%9a%e4%bb%8e%e5%ba%95%e5%b1%82%e5%8e%9f%e7%90%86%e5%88%b0%e5%ae%9e%e6%88%98%e9%81%bf%e5%9d%91%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/77459.html","title":{"rendered":"15. Java\u5f02\u5e38\u5904\u7406\u5168\u89e3\u6790\uff1a\u4ece\u5e95\u5c42\u539f\u7406\u5230\u5b9e\u6218\u907f\u5751\u6307\u5357"},"content":{"rendered":"<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"1.%20%E5%BC%82%E5%B8%B8%E7%9A%84%E6%A6%82%E5%BF%B5%E4%B8%8E%E4%BD%93%E7%B3%BB%E7%BB%93%E6%9E%84-toc\" style=\"margin-left:0px\">1. \u5f02\u5e38\u7684\u6982\u5ff5\u4e0e\u4f53\u7cfb\u7ed3\u6784<\/p>\n<p id=\"1.1%20%E5%BC%82%E5%B8%B8%E7%9A%84%E6%A6%82%E5%BF%B5-toc\" style=\"margin-left:40px\">1.1 \u5f02\u5e38\u7684\u6982\u5ff5<\/p>\n<p id=\"1.1.1%20%E7%AE%97%E6%9C%AF%E5%BC%82%E5%B8%B8-toc\" style=\"margin-left:80px\">1.1.1 \u7b97\u672f\u5f02\u5e38<\/p>\n<p id=\"1.1.2%C2%A0%E6%95%B0%E7%BB%84%E8%B6%8A%E7%95%8C%E5%BC%82%E5%B8%B8-toc\" style=\"margin-left:80px\">1.1.2\u00a0\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38<\/p>\n<p id=\"1.1.3%20%E7%A9%BA%E6%8C%87%E9%92%88%E5%BC%82%E5%B8%B8-toc\" style=\"margin-left:80px\">1.1.3 \u7a7a\u6307\u9488\u5f02\u5e38<\/p>\n<p id=\"1.2%20%E5%BC%82%E5%B8%B8%E7%9A%84%E4%BD%93%E7%B3%BB%E7%BB%93%E6%9E%84-toc\" style=\"margin-left:40px\">1.2 \u5f02\u5e38\u7684\u4f53\u7cfb\u7ed3\u6784<\/p>\n<p id=\"1.3%20%E5%BC%82%E5%B8%B8%E7%9A%84%E5%88%86%E7%B1%BB-toc\" style=\"margin-left:40px\">1.3 \u5f02\u5e38\u7684\u5206\u7c7b<\/p>\n<p id=\"1.3.1%20%E7%BC%96%E8%AF%91%E6%97%B6%E5%BC%82%E5%B8%B8-toc\" style=\"margin-left:80px\">1.3.1 \u7f16\u8bd1\u65f6\u5f02\u5e38<\/p>\n<p id=\"1.3.1.1%20%E4%BE%8B%E5%AD%901%EF%BC%9A-toc\" style=\"margin-left:80px\">1.3.1.1 \u4f8b\u5b501&#xff1a;<\/p>\n<p id=\"1.3.1.2%20%E4%BE%8B%E5%AD%902%EF%BC%9A-toc\" style=\"margin-left:80px\">1.3.1.2 \u4f8b\u5b502&#xff1a;<\/p>\n<p id=\"1.3.2%C2%A0%E8%BF%90%E8%A1%8C%E6%97%B6%E5%BC%82%E5%B8%B8-toc\" style=\"margin-left:80px\">1.3.2\u00a0\u8fd0\u884c\u65f6\u5f02\u5e38<\/p>\n<p id=\"2.%20%E5%BC%82%E5%B8%B8%E7%9A%84%E5%A4%84%E7%90%86-toc\" style=\"margin-left:0px\">2. \u5f02\u5e38\u7684\u5904\u7406<\/p>\n<p id=\"2.1%20%E9%98%B2%E5%BE%A1%E5%BC%8F%E7%BC%96%E7%A8%8B-toc\" style=\"margin-left:40px\">2.1 \u9632\u5fa1\u5f0f\u7f16\u7a0b<\/p>\n<p id=\"2.1.1%20LBYL%3A%20Look%20Before%20You%20Leap.%20%E5%9C%A8%E6%93%8D%E4%BD%9C%E4%B9%8B%E5%89%8D%E5%B0%B1%E5%81%9A%E5%85%85%E5%88%86%E7%9A%84%E6%A3%80%E6%9F%A5%E3%80%82%E5%8D%B3%EF%BC%9A%E4%BA%8B%E5%89%8D%E9%98%B2%E5%BE%A1%E5%9E%8B-toc\" style=\"margin-left:80px\">2.1.1 LBYL: Look Before You Leap. \u5728\u64cd\u4f5c\u4e4b\u524d\u5c31\u505a\u5145\u5206\u7684\u68c0\u67e5\u3002\u5373&#xff1a;\u4e8b\u524d\u9632\u5fa1\u578b<\/p>\n<p id=\"2.1.2%20EAFP%3A%20It&#039;s%20Easier%20to%20Ask%20Forgiveness%20than%20Permission.%20%22%E4%BA%8B%E5%90%8E%E8%8E%B7%E5%8F%96%E5%8E%9F%E8%B0%85%E6%AF%94%E4%BA%8B%E5%89%8D%E8%8E%B7%E5%8F%96%E8%AE%B8%E5%8F%AF%E6%9B%B4%E5%AE%B9%E6%98%93%22.%20%E4%B9%9F%E5%B0%B1%E6%98%AF%E5%85%88%E6%93%8D%E4%BD%9C%2C%20%E9%81%87%E5%88%B0%E9%97%AE%E9%A2%98%E5%86%8D%E5%A4%84%E7%90%86.%20%E5%8D%B3%3A%20%E4%BA%8B%E5%90%8E%E8%AE%A4%E9%94%99%E5%9E%8B-toc\" style=\"margin-left:80px\">2.1.2 EAFP: It&#039;s Easier to Ask Forgiveness than Permission. &#034;\u4e8b\u540e\u83b7\u53d6\u539f\u8c05\u6bd4\u4e8b\u524d\u83b7\u53d6\u8bb8\u53ef\u66f4\u5bb9\u6613&#034;. \u4e5f\u5c31\u662f\u5148\u64cd\u4f5c, \u9047\u5230\u95ee\u9898\u518d\u5904\u7406. \u5373: \u4e8b\u540e\u8ba4\u9519\u578b<\/p>\n<p id=\"2.2%20%E5%BC%82%E5%B8%B8%E7%9A%84%E6%8A%9B%E5%87%BA-throw-toc\" style=\"margin-left:40px\">2.2 \u5f02\u5e38\u7684\u629b\u51fa-throw<\/p>\n<p id=\"2.3%20%E5%BC%82%E5%B8%B8%E7%9A%84%E5%A3%B0%E6%98%8E-throws-toc\" style=\"margin-left:40px\">2.3 \u5f02\u5e38\u7684\u58f0\u660e-throws<\/p>\n<p id=\"2.4%20%E5%BC%82%E5%B8%B8%E7%9A%84%E6%8D%95%E8%8E%B7-try-catch%E6%8D%95%E8%8E%B7%E5%B9%B6%E5%A4%84%E7%90%86%E5%BC%82%E5%B8%B8-toc\" style=\"margin-left:40px\">2.4 \u5f02\u5e38\u7684\u6355\u83b7-try-catch\u6355\u83b7\u5e76\u5904\u7406\u5f02\u5e38<\/p>\n<p id=\"%E3%80%90%E5%85%B3%E4%BA%8E%E5%BC%82%E5%B8%B8%E7%9A%84%E5%A4%84%E7%90%86%E6%96%B9%E5%BC%8F%E3%80%911%E3%80%812%E3%80%813-toc\" style=\"margin-left:80px\">\u3010\u5173\u4e8e\u5f02\u5e38\u7684\u5904\u7406\u65b9\u5f0f\u30111\u30012\u30013<\/p>\n<p id=\"%E3%80%90%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9%E3%80%911%E3%80%812%E3%80%813%E3%80%814%E3%80%815-toc\" style=\"margin-left:80px\">\u3010\u6ce8\u610f\u4e8b\u9879\u30111\u30012\u30013\u30014\u30015<\/p>\n<p id=\"2.5%20finally-toc\" style=\"margin-left:40px\">2.5 finally<\/p>\n<p id=\"2.6%20%E5%BC%82%E5%B8%B8%E7%9A%84%E5%A4%84%E7%90%86%E6%B5%81%E7%A8%8B-toc\" style=\"margin-left:40px\">2.6 \u5f02\u5e38\u7684\u5904\u7406\u6d41\u7a0b<\/p>\n<p id=\"3.%20%E8%87%AA%E5%AE%9A%E4%B9%89%E5%BC%82%E5%B8%B8-toc\" style=\"margin-left:0px\">3. \u81ea\u5b9a\u4e49\u5f02\u5e38<\/p>\n<p id=\"4.%20%E5%B0%8F%E8%AF%95%E7%89%9B%E5%88%80%EF%BC%889%E9%81%93%E9%A2%98%EF%BC%89-toc\" style=\"margin-left:0px\">4. \u5c0f\u8bd5\u725b\u5200&#xff08;9\u9053\u9898&#xff09;<\/p>\n<hr id=\"hr-toc\" \/>\n<p><span style=\"background-color:#f9eda6\">17.2025-03-24-\u5f02\u5e38 &#043; 18.2025-03-26\u5f02\u5e38\u5b8c<\/span><\/p>\n<p>15. \u5f02\u5e38<\/p>\n<p>\u3010\u672c\u7ae0\u76ee\u6807\u3011<\/p>\n<li>\n<p>\u5f02\u5e38\u6982\u5ff5\u4e0e\u4f53\u7cfb\u7ed3\u6784<\/p>\n<\/li>\n<li>\n<p>\u5f02\u5e38\u7684\u5904\u7406\u65b9\u5f0f<\/p>\n<\/li>\n<li>\n<p>\u5f02\u5e38\u7684\u5904\u7406\u6d41\u7a0b<\/p>\n<\/li>\n<li>\n<p>\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b<\/p>\n<\/li>\n<h2 id=\"1.%20%E5%BC%82%E5%B8%B8%E7%9A%84%E6%A6%82%E5%BF%B5%E4%B8%8E%E4%BD%93%E7%B3%BB%E7%BB%93%E6%9E%84\">1. \u5f02\u5e38\u7684\u6982\u5ff5\u4e0e\u4f53\u7cfb\u7ed3\u6784<\/h2>\n<h3 id=\"1.1%20%E5%BC%82%E5%B8%B8%E7%9A%84%E6%A6%82%E5%BF%B5\">1.1 \u5f02\u5e38\u7684\u6982\u5ff5<\/h3>\n<p>\u5f02\u5e38\u662f\u5728\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u53d1\u751f\u7684\u4e00\u79cd\u7279\u6b8a\u60c5\u51b5\u6216\u9519\u8bef\u72b6\u6001\u3002\u5b83\u6253\u65ad\u4e86\u7a0b\u5e8f\u7684\u6b63\u5e38\u6d41\u7a0b&#xff0c;\u9700\u8981\u7279\u522b\u5904\u7406\u3002<\/p>\n<p>\u5728\u65e5\u5e38\u5f00\u53d1\u4e2d&#xff0c;\u7a0b\u5e8f\u5458\u7ede\u5c3d\u8111\u6c41\u5c06\u4ee3\u7801\u5199\u7684\u5c3d\u5584\u5c3d\u7f8e&#xff0c;\u5728\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d&#xff0c;\u96be\u514d\u4f1a\u51fa\u73b0\u4e00\u4e9b\u5947\u5947\u602a\u602a\u7684\u95ee\u9898\u3002\u6709\u65f6\u901a\u8fc7\u4ee3\u7801\u5f88\u96be\u53bb\u63a7\u5236&#xff0c;\u6bd4\u5982&#xff1a;\u6570\u636e\u683c\u5f0f\u4e0d\u5bf9\u3001\u7f51\u7edc\u4e0d\u901a\u7545\u3001\u5185\u5b58\u62a5\u8b66\u7b49\u3002<\/p>\n<p>\u5728Java\u4e2d&#xff0c;\u5c06\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u53d1\u751f\u7684\u4e0d\u6b63\u5e38\u884c\u4e3a\u79f0\u4e3a\u5f02\u5e38&#xff0c;\u6bd4\u5982\u4e4b\u524d\u5199\u4ee3\u7801\u65f6\u7ecf\u5e38\u9047\u5230\u7684&#xff1a;<\/p>\n<h4 id=\"1.1.1%20%E7%AE%97%E6%9C%AF%E5%BC%82%E5%B8%B8\">1.1.1 \u7b97\u672f\u5f02\u5e38<\/h4>\n<p>public static void main(String[] args) {<br \/>\n    System.out.println(10 \/ 0);<br \/>\n}<br \/>\nException in thread &#034;main&#034; java.lang.ArithmeticException: \/ by zero<\/p>\n<p>at LibrarySystem.main(LibrarySystem.java:97) <\/p>\n<h4 id=\"1.1.2%C2%A0%E6%95%B0%E7%BB%84%E8%B6%8A%E7%95%8C%E5%BC%82%E5%B8%B8\">1.1.2\u00a0\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38<\/h4>\n<p>public static void main(String[] args) {<br \/>\n    int[] array &#061; {1, 2, 3};<br \/>\n    System.out.println(array[100]);<br \/>\n}<br \/>\nException in thread &#034;main&#034; java.lang.ArrayIndexOutOfBoundsException: Index 100 out of bounds for length 3<\/p>\n<p>at LibrarySystem.main(LibrarySystem.java:98) <\/p>\n<h4 id=\"1.1.3%20%E7%A9%BA%E6%8C%87%E9%92%88%E5%BC%82%E5%B8%B8\">1.1.3 \u7a7a\u6307\u9488\u5f02\u5e38<\/h4>\n<p>public static void main(String[] args) {<br \/>\n    int[] array &#061; null;<br \/>\n    System.out.println(array.length);<br \/>\n}<br \/>\nException in thread &#034;main&#034; java.lang.NullPointerException: Cannot read the array length because &#034;array&#034; is null<\/p>\n<p>at LibrarySystem.main(LibrarySystem.java:98) <\/p>\n<p>\u4ece\u4e0a\u8ff0\u8fc7\u7a0b\u4e2d\u53ef\u4ee5\u770b\u5230&#xff0c;java\u4e2d\u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38&#xff0c;\u90fd\u6709\u4e0e\u5176\u5bf9\u5e94\u7684\u7c7b\u6765\u8fdb\u884c\u63cf\u8ff0\u3002<\/p>\n<h3 id=\"1.2%20%E5%BC%82%E5%B8%B8%E7%9A%84%E4%BD%93%E7%B3%BB%E7%BB%93%E6%9E%84\">1.2 \u5f02\u5e38\u7684\u4f53\u7cfb\u7ed3\u6784<\/h3>\n<p>\u5f02\u5e38\u79cd\u7c7b\u7e41\u591a&#xff0c;\u4e3a\u4e86\u5bf9\u4e0d\u540c\u5f02\u5e38\u6216\u8005\u9519\u8bef\u8fdb\u884c\u5f88\u597d\u7684\u5206\u7c7b\u7ba1\u7406&#xff0c;Java\u5185\u90e8\u7ef4\u62a4\u4e86\u4e00\u4e2a\u5f02\u5e38\u7684\u4f53\u7cfb\u7ed3\u6784&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"619\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260224104323-699d80cb38a5c.png\" width=\"1483\" \/><\/p>\n<p>\u4ece\u4e0a\u56fe\u4e2d\u53ef\u4ee5\u770b\u5230&#xff1a;<\/p>\n<li>\n<p>Throwable&#xff1a;\u662f\u5f02\u5e38\u4f53\u7cfb\u7684\u9876\u5c42\u7c7b&#xff0c;\u5176\u6d3e\u751f\u51fa\u4e24\u4e2a\u91cd\u8981\u7684\u5b50\u7c7b&#xff0c;Error \u548c Exception<\/p>\n<\/li>\n<li>\n<p>Error&#xff1a;\u6307\u7684\u662fJava\u865a\u62df\u673a\u65e0\u6cd5\u89e3\u51b3\u7684\u4e25\u91cd\u95ee\u9898&#xff0c;\u6bd4\u5982&#xff1a;JVM\u7684\u5185\u90e8\u9519\u8bef\u3001\u8d44\u6e90\u8017\u5c3d\u7b49&#xff0c;\u5178\u578b\u4ee3\u8868&#xff1a;StackOverflowError\u548cOutOfMemoryError&#xff0c;\u4e00\u65e6\u53d1\u751f\u56de\u529b\u4e4f\u672f\u3002<\/p>\n<\/li>\n<li>\n<p>Exception&#xff1a;\u5f02\u5e38\u4ea7\u751f\u540e\u7a0b\u5e8f\u5458\u53ef\u4ee5\u901a\u8fc7\u4ee3\u7801\u8fdb\u884c\u5904\u7406&#xff0c;\u4f7f\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c\u3002\u6bd4\u5982&#xff1a;\u611f\u5192\u3001\u53d1\u70e7\u3002\u6211\u4eec\u5e73\u65f6\u6240\u8bf4\u7684\u5f02\u5e38\u5c31\u662fException\u3002<\/p>\n<\/li>\n<h3 id=\"1.3%20%E5%BC%82%E5%B8%B8%E7%9A%84%E5%88%86%E7%B1%BB\">1.3 \u5f02\u5e38\u7684\u5206\u7c7b<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"220\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260224104323-699d80cb88f4e.png\" width=\"458\" \/><\/p>\n<p>\u5f02\u5e38\u53ef\u80fd\u5728\u7f16\u8bd1\u65f6\u53d1\u751f&#xff0c;\u4e5f\u53ef\u80fd\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u53d1\u751f&#xff0c;\u6839\u636e\u53d1\u751f\u7684\u65f6\u673a\u4e0d\u540c&#xff0c;\u53ef\u4ee5\u5c06\u5f02\u5e38\u5206\u4e3a&#xff1a;<\/p>\n<h4 id=\"1.3.1%20%E7%BC%96%E8%AF%91%E6%97%B6%E5%BC%82%E5%B8%B8\">1.3.1 \u7f16\u8bd1\u65f6\u5f02\u5e38<\/h4>\n<h4 id=\"1.3.1.1%20%E4%BE%8B%E5%AD%901%EF%BC%9A\"><span style=\"color:#be191c\">1.3.1.1 \u4f8b\u5b501&#xff1a;<\/span><\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"537\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260224104323-699d80cb9ea4d.png\" width=\"771\" \/><\/p>\n<p><span style=\"color:#be191c\">\u5904\u7406\u5f02\u5e38&#8211;throws&#xff1a;<\/span><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"401\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260224104324-699d80cc04e12.png\" width=\"806\" \/><\/p>\n<h4 id=\"1.3.1.2%20%E4%BE%8B%E5%AD%902%EF%BC%9A\"><span style=\"color:#be191c\">1.3.1.2 \u4f8b\u5b502&#xff1a;<\/span><\/h4>\n<p> import java.io.FileInputStream;<br \/>\nimport java.io.FileNotFoundException;<\/p>\n<p>public class CheckedExceptionExample {<br \/>\n    public static void main(String[] args) {<br \/>\n        try {<br \/>\n            \/\/ \u5c1d\u8bd5\u6253\u5f00\u4e00\u4e2a\u53ef\u80fd\u4e0d\u5b58\u5728\u7684\u6587\u4ef6<br \/>\n            FileInputStream fis &#061; new FileInputStream(&#034;hello.txt&#034;);<br \/>\n            System.out.println(&#034;\u6587\u4ef6\u6253\u5f00\u6210\u529f&#034;);<br \/>\n        } catch (FileNotFoundException e) {<br \/>\n            \/\/ \u6355\u83b7\u5e76\u5904\u7406\u6587\u4ef6\u672a\u627e\u5230\u5f02\u5e38<br \/>\n            System.out.println(&#034;\u9519\u8bef&#xff1a;\u6587\u4ef6\u4e0d\u5b58\u5728&#034;);<br \/>\n            e.printStackTrace();<br \/>\n        }<br \/>\n    }<br \/>\n} <\/p>\n<p>\u5728 Java \u4e2d&#xff0c;FileInputStream\u6784\u9020\u65b9\u6cd5\u5728\u7f16\u8bd1\u65f6\u4f1a\u5f3a\u5236\u68c0\u67e5 FileNotFoundException&#xff0c;\u8fd9\u5c5e\u4e8e\u53d7\u67e5\u5f02\u5e38&#xff08;Checked Exception&#xff09;\u3002\u7531\u4e8e\u53d7\u67e5\u5f02\u5e38\u5fc5\u987b\u5728\u7f16\u8bd1\u9636\u6bb5\u88ab\u663e\u5f0f\u5904\u7406&#xff0c;\u4ee3\u7801\u4e2d\u4f7f\u7528\u4e86 try-catch\u5757\u6765\u6355\u83b7\u8be5\u5f02\u5e38&#xff0c;\u786e\u4fdd\u5373\u4f7f\u6587\u4ef6\u4e0d\u5b58\u5728\u7a0b\u5e8f\u4e5f\u80fd\u7ed9\u51fa\u660e\u786e\u7684\u9519\u8bef\u63d0\u793a\u800c\u975e\u76f4\u63a5\u5d29\u6e83\u3002\u8fd9\u79cd\u673a\u5236\u8981\u6c42\u5f00\u53d1\u8005\u5fc5\u987b\u8003\u8651\u5e76\u5904\u7406\u6f5c\u5728\u7684\u9519\u8bef\u60c5\u51b5&#xff0c;\u4ece\u800c\u589e\u5f3a\u4ee3\u7801\u7684\u5065\u58ee\u6027\u3002<\/p>\n<p>\u5728\u7a0b\u5e8f\u7f16\u8bd1\u671f\u95f4\u53d1\u751f\u7684\u5f02\u5e38&#xff0c;\u79f0\u4e3a\u7f16\u8bd1\u65f6\u5f02\u5e38&#xff0c;\u4e5f\u79f0\u4e3a\u53d7\u68c0\u67e5\u5f02\u5e38(Checked Exception)<\/p>\n<p>public class Person {<br \/>\n    private String name;<br \/>\n    private String gender;<br \/>\n    int age;<\/p>\n<p>    \/\/ \u60f3\u8981\u8ba9\u8be5\u7c7b\u652f\u6301\u6df1\u62f7\u8d1d&#xff0c;\u8986\u5199Object\u7c7b\u7684clone\u65b9\u6cd5\u5373\u53ef<\/p>\n<p>    &#064;Override<br \/>\n    public Person clone() {<br \/>\n        return (Person)super.clone();<br \/>\n    }<br \/>\n} <\/p>\n<p>\u7f16\u8bd1\u65f6\u62a5\u9519&#xff1a;<\/p>\n<p>Error:(17, 35) java: \u672a\u62a5\u544a\u7684\u5f02\u5e38\u9519\u8befjava.lang.CloneNotSupportedException;<\/p>\n<p>\u5fc5\u987b\u5bf9\u5176\u8fdb\u884c\u6355\u83b7\u6216\u58f0\u660e\u4ee5\u4fbf\u629b\u51fa<\/p>\n<h4 id=\"1.3.2%C2%A0%E8%BF%90%E8%A1%8C%E6%97%B6%E5%BC%82%E5%B8%B8\">1.3.2\u00a0\u8fd0\u884c\u65f6\u5f02\u5e38<\/h4>\n<p>\u5728\u7a0b\u5e8f\u6267\u884c\u671f\u95f4\u53d1\u751f\u7684\u5f02\u5e38&#xff0c;\u79f0\u4e3a\u8fd0\u884c\u65f6\u5f02\u5e38&#xff0c;\u4e5f\u79f0\u4e3a\u975e\u53d7\u68c0\u67e5\u5f02\u5e38(Unchecked Exception)<\/p>\n<p><span style=\"background-color:#cbe0f1\">RuntimeException\u4ee5\u53ca\u5176\u5b50\u7c7b\u5bf9\u5e94\u7684\u5f02\u5e38&#xff0c;\u90fd\u79f0\u4e3a\u8fd0\u884c\u65f6\u5f02\u5e38\u3002<\/span>\u6bd4\u5982&#xff1a;NullPointerException\u3001ArrayIndexOutOfBoundsException\u3001ArithmeticException\u3002<\/p>\n<p>NullPointerException&#xff1a;<\/p>\n<p> int[] array &#061; null;<br \/>\nSystem.out.println(array.length);<br \/>\n\/\/\u7f16\u8bd1\u65f6\u4e0d\u62a5\u9519&#xff0c;\u8fd0\u884c\u540e\u624d\u4f1a\u62a5\u9519 <\/p>\n<p><span style=\"color:#be191c\">\u6ce8\u610f&#xff1a;<\/span>\u7f16\u8bd1\u65f6\u51fa\u73b0\u7684\u8bed\u6cd5\u6027\u9519\u8bef&#xff0c;\u4e0d\u80fd\u79f0\u4e4b\u4e3a\u5f02\u5e38\u3002\u4f8b\u5982\u5c06 System.out.println \u62fc\u5199\u9519\u4e86&#xff0c;\u5199\u6210\u4e86 system.out.println\u3002\u6b64\u65f6\u7f16\u8bd1\u8fc7\u7a0b\u4e2d\u5c31\u4f1a\u51fa\u9519&#xff0c;\u8fd9\u662f\u201c\u7f16\u8bd1\u671f\u201d\u51fa\u9519\u3002\u800c\u8fd0\u884c\u65f6\u6307\u7684\u662f\u7a0b\u5e8f\u5df2\u7ecf\u7f16\u8bd1\u901a\u8fc7\u5f97\u5230 class \u6587\u4ef6\u4e86&#xff0c;\u518d\u7531 JVM \u6267\u884c\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u7684\u9519\u8bef\u3002<\/p>\n<hr \/>\n<h2 id=\"2.%20%E5%BC%82%E5%B8%B8%E7%9A%84%E5%A4%84%E7%90%86\">2. \u5f02\u5e38\u7684\u5904\u7406<\/h2>\n<h3 id=\"2.1%20%E9%98%B2%E5%BE%A1%E5%BC%8F%E7%BC%96%E7%A8%8B\">2.1 \u9632\u5fa1\u5f0f\u7f16\u7a0b<\/h3>\n<p>\u9519\u8bef\u5728\u4ee3\u7801\u4e2d\u662f\u5ba2\u89c2\u5b58\u5728\u7684\u3002\u56e0\u6b64\u6211\u4eec\u8981\u8ba9\u7a0b\u5e8f\u51fa\u73b0\u95ee\u9898\u7684\u65f6\u5019\u53ca\u65f6\u901a\u77e5\u7a0b\u5e8f\u5458\u3002\u4e3b\u8981\u7684\u65b9\u5f0f<\/p>\n<h4 id=\"2.1.1%20LBYL%3A%20Look%20Before%20You%20Leap.%20%E5%9C%A8%E6%93%8D%E4%BD%9C%E4%B9%8B%E5%89%8D%E5%B0%B1%E5%81%9A%E5%85%85%E5%88%86%E7%9A%84%E6%A3%80%E6%9F%A5%E3%80%82%E5%8D%B3%EF%BC%9A%E4%BA%8B%E5%89%8D%E9%98%B2%E5%BE%A1%E5%9E%8B\">2.1.1 LBYL: Look Before You Leap. \u5728\u64cd\u4f5c\u4e4b\u524d\u5c31\u505a\u5145\u5206\u7684\u68c0\u67e5\u3002\u5373&#xff1a;\u4e8b\u524d\u9632\u5fa1\u578b<\/h4>\n<p>boolean ret &#061; false;<br \/>\nret &#061; \u767b\u9646\u6e38\u620f();<br \/>\nif (!ret) {<br \/>\n    \u5904\u7406\u767b\u9646\u6e38\u620f\u9519\u8bef;<br \/>\n    return;<br \/>\n}<br \/>\nret &#061; \u5f00\u59cb\u5339\u914d();<br \/>\nif (!ret) {<br \/>\n    \u5904\u7406\u5339\u914d\u9519\u8bef;<br \/>\n    return;<br \/>\n}<br \/>\nret &#061; \u6e38\u620f\u786e\u8ba4();<br \/>\nif (!ret) {<br \/>\n    \u5904\u7406\u6e38\u620f\u786e\u8ba4\u9519\u8bef;<br \/>\n    return;<br \/>\n}<br \/>\nret &#061; \u9009\u62e9\u82f1\u96c4();<br \/>\nif (!ret) {<br \/>\n    \u5904\u7406\u9009\u62e9\u82f1\u96c4\u9519\u8bef;<br \/>\n    return;<br \/>\n}<br \/>\nret &#061; \u8f7d\u5165\u6e38\u620f\u753b\u9762();<br \/>\nif (!ret) {<br \/>\n    \u5904\u7406\u8f7d\u5165\u6e38\u620f\u9519\u8bef;<br \/>\n    return;<br \/>\n}<br \/>\n&#8230;&#8230; <\/p>\n<p>\u7f3a\u9677&#xff1a;\u6b63\u5e38\u6d41\u7a0b\u548c\u9519\u8bef\u5904\u7406\u6d41\u7a0b\u4ee3\u7801\u6df7\u5728\u4e00\u8d77&#xff0c;\u4ee3\u7801\u6574\u4f53\u663e\u7684\u6bd4\u8f83\u6df7\u4e71\u3002<\/p>\n<h4 id=\"2.1.2%20EAFP%3A%20It&#039;s%20Easier%20to%20Ask%20Forgiveness%20than%20Permission.%20%22%E4%BA%8B%E5%90%8E%E8%8E%B7%E5%8F%96%E5%8E%9F%E8%B0%85%E6%AF%94%E4%BA%8B%E5%89%8D%E8%8E%B7%E5%8F%96%E8%AE%B8%E5%8F%AF%E6%9B%B4%E5%AE%B9%E6%98%93%22.%20%E4%B9%9F%E5%B0%B1%E6%98%AF%E5%85%88%E6%93%8D%E4%BD%9C%2C%20%E9%81%87%E5%88%B0%E9%97%AE%E9%A2%98%E5%86%8D%E5%A4%84%E7%90%86.%20%E5%8D%B3%3A%20%E4%BA%8B%E5%90%8E%E8%AE%A4%E9%94%99%E5%9E%8B\">2.1.2 EAFP: It&#039;s Easier to Ask Forgiveness than Permission. &#034;\u4e8b\u540e\u83b7\u53d6\u539f\u8c05\u6bd4\u4e8b\u524d\u83b7\u53d6\u8bb8\u53ef\u66f4\u5bb9\u6613&#034;. \u4e5f\u5c31\u662f\u5148\u64cd\u4f5c, \u9047\u5230\u95ee\u9898\u518d\u5904\u7406. \u5373: \u4e8b\u540e\u8ba4\u9519\u578b<\/h4>\n<p>try {<br \/>\n    \u767b\u9646\u6e38\u620f();<br \/>\n    \u5f00\u59cb\u5339\u914d();<br \/>\n    \u6e38\u620f\u786e\u8ba4();<br \/>\n    \u9009\u62e9\u82f1\u96c4();<br \/>\n    \u8f7d\u5165\u6e38\u620f\u753b\u9762();<br \/>\n    &#8230;<br \/>\n} catch (\u767b\u9646\u6e38\u620f\u5f02\u5e38) {<br \/>\n    \u5904\u7406\u767b\u9646\u6e38\u620f\u5f02\u5e38;<br \/>\n} catch (\u5f00\u59cb\u5339\u914d\u5f02\u5e38) {<br \/>\n    \u5904\u7406\u5f00\u59cb\u5339\u914d\u5f02\u5e38;<br \/>\n} catch (\u6e38\u620f\u786e\u8ba4\u5f02\u5e38) {<br \/>\n    \u5904\u7406\u6e38\u620f\u786e\u8ba4\u5f02\u5e38;<br \/>\n    &#8230;<br \/>\n} catch (\u9009\u62e9\u82f1\u96c4\u5f02\u5e38) {<br \/>\n    \u5904\u7406\u9009\u62e9\u82f1\u96c4\u5f02\u5e38;<br \/>\n} catch (\u8f7d\u5165\u6e38\u620f\u753b\u9762\u5f02\u5e38) {<br \/>\n    \u5904\u7406\u8f7d\u5165\u6e38\u620f\u5f02\u5e38;<br \/>\n    &#8230;&#8230;<br \/>\n} <\/p>\n<p>\u4f18\u52bf&#xff1a;\u6b63\u5e38\u6d41\u7a0b\u548c\u9519\u8bef\u6d41\u7a0b\u662f\u5206\u79bb\u5f00\u7684, \u7a0b\u5e8f\u5458\u66f4\u5173\u6ce8\u6b63\u5e38\u6d41\u7a0b, \u4ee3\u7801\u66f4\u6e05\u6670, \u5bb9\u6613\u7406\u89e3\u4ee3\u7801<\/p>\n<p>\u5f02\u5e38\u5904\u7406\u7684\u6838\u5fc3\u601d\u60f3\u5c31\u662f EAFP\u3002<\/p>\n<p>\u5728Java\u4e2d, \u5f02\u5e38\u5904\u7406\u4e3b\u8981\u76845\u4e2a\u5173\u952e\u5b57: <span style=\"color:#be191c\">throw\u3001try\u3001catch\u3001finally\u3001throws\u3002<\/span><\/p>\n<h3 id=\"2.2%20%E5%BC%82%E5%B8%B8%E7%9A%84%E6%8A%9B%E5%87%BA-throw\">2.2 \u5f02\u5e38\u7684\u629b\u51fa-throw<\/h3>\n<p>\u5728\u7f16\u5199\u7a0b\u5e8f\u65f6&#xff0c;\u5982\u679c\u7a0b\u5e8f\u4e2d\u51fa\u73b0\u9519\u8bef&#xff0c;\u6b64\u65f6\u5c31\u9700\u8981\u5c06\u9519\u8bef\u7684\u4fe1\u606f\u544a\u77e5\u7ed9\u8c03\u7528\u8005&#xff0c;\u6bd4\u5982&#xff1a;\u53c2\u6570\u68c0\u6d4b\u3002<\/p>\n<p>\u5728Java\u4e2d&#xff0c;\u53ef\u4ee5\u501f\u52a9throw\u5173\u952e\u5b57&#xff0c;\u629b\u51fa\u4e00\u4e2a\u6307\u5b9a\u7684\u5f02\u5e38\u5bf9\u8c61&#xff0c;\u5c06\u9519\u8bef\u4fe1\u606f\u544a\u77e5\u7ed9\u8c03\u7528\u8005\u3002\u5177\u4f53\u8bed\u6cd5\u5982\u4e0b&#xff1a;<\/p>\n<p>throw new XXXException(&#034;\u5f02\u5e38\u4ea7\u751f\u7684\u539f\u56e0&#034;); <\/p>\n<p>\u3010\u9700\u6c42\u3011&#xff1a;\u5b9e\u73b0\u4e00\u4e2a\u83b7\u53d6\u6570\u7ec4\u4e2d\u4efb\u610f\u4f4d\u7f6e\u5143\u7d20\u7684\u65b9\u6cd5\u3002<\/p>\n<p>public static int getElement(int[] array, int index){<br \/>\n    if(null &#061;&#061; array){<br \/>\n        throw new NullPointerException(&#034;\u4f20\u9012\u7684\u6570\u7ec4\u4e3anull&#034;);<br \/>\n    }<\/p>\n<p>    if(index &lt; 0 || index &gt;&#061; array.length){<br \/>\n        throw new ArrayIndexOutOfBoundsException(&#034;\u4f20\u9012\u7684\u6570\u7ec4\u4e0b\u6807\u8d8a\u754c&#034;);<br \/>\n    }<\/p>\n<p>    return array[index];<br \/>\n}<\/p>\n<p>public static void main(String[] args) {<br \/>\n    int[] array &#061; {1,2,3};<br \/>\n    getElement(array, 3);<br \/>\n} <\/p>\n<p>\u3010\u6ce8\u610f\u4e8b\u9879\u3011<\/p>\n<li>\n<p>throw\u5fc5\u987b\u5199\u5728\u65b9\u6cd5\u4f53\u5185\u90e8<\/p>\n<\/li>\n<li>\n<p>\u629b\u51fa\u7684\u5bf9\u8c61\u5fc5\u987b&#xff08;\u5728\u5f02\u5e38\u7684\u8303\u7574\u91cc\u9762&#xff09;\u662fException \u6216\u8005 Exception \u7684\u5b50\u7c7b\u5bf9\u8c61<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u629b\u51fa\u7684\u662f RunTimeException \u6216\u8005 RunTimeException \u7684\u5b50\u7c7b&#xff0c;\u5219\u53ef\u4ee5\u4e0d\u7528\u5904\u7406&#xff0c;\u76f4\u63a5\u4ea4\u7ed9 JVM\u6765\u5904\u7406<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u629b\u51fa\u7684\u662f\u7f16\u8bd1\u65f6\u5f02\u5e38&#xff0c;\u7528\u6237\u5fc5\u987b\u5904\u7406&#xff0c;\u5426\u5219\u65e0\u6cd5\u901a\u8fc7\u7f16\u8bd1<\/p>\n<\/li>\n<li>\n<p><span style=\"color:#be191c\">\u5f02\u5e38\u4e00\u65e6\u629b\u51fa&#xff0c;\u5176\u540e\u7684\u4ee3\u7801\u5c31\u4e0d\u4f1a\u6267\u884c<\/span><\/p>\n<\/li>\n<h3 id=\"2.3%20%E5%BC%82%E5%B8%B8%E7%9A%84%E5%A3%B0%E6%98%8E-throws\">2.3 \u5f02\u5e38\u7684\u58f0\u660e-throws<\/h3>\n<p>throws \u5173\u952e\u5b57\u7528\u4e8e\u5728\u65b9\u6cd5\u58f0\u660e\u4e2d\u5217\u51fa\u8be5\u65b9\u6cd5\u53ef\u80fd\u629b\u51fa\u7684\u5f02\u5e38&#xff0c;\u5b83\u544a\u8bc9\u8c03\u7528\u8005\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u80fd\u4f1a\u629b\u51fa\u67d0\u4e9b\u5f02\u5e38&#xff0c;\u8c03\u7528\u8005\u9700\u8981\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002\u4f7f\u7528 throws \u5b9e\u9645\u4e0a\u662f\u5c06\u5f02\u5e38\u7684\u5904\u7406\u8d23\u4efb\u8f6c\u79fb\u7ed9\u4e86\u8c03\u7528\u8be5\u65b9\u6cd5\u7684\u4ee3\u7801\u3002<\/p>\n<p>\u8bed\u6cd5\u683c\u5f0f&#xff1a;<\/p>\n<p>\u4fee\u9970\u7b26 \u8fd4\u56de\u503c\u7c7b\u578b \u65b9\u6cd5\u540d(\u53c2\u6570\u5217\u8868) <span style=\"color:#be191c\">throws<\/span> \u5f02\u5e38\u7c7b\u578b1, \u5f02\u5e38\u7c7b\u578b2&#8230;{<\/p>\n<p>}<\/p>\n<p>\u9700\u6c42&#xff1a;\u52a0\u8f7d\u6307\u5b9a\u7684\u914d\u7f6e\u6587\u4ef6config.ini<\/p>\n<p>public class Config {<br \/>\n    File file;<\/p>\n<p>    \/*<br \/>\n    FileNotFoundException : \u7f16\u8bd1\u65f6\u5f02\u5e38&#xff0c;\u8868\u660e\u6587\u4ef6\u4e0d\u5b58\u5728<br \/>\n    \u6b64\u5904\u4e0d\u5904\u7406&#xff0c;\u4e5f\u6ca1\u6709\u80fd\u529b\u5904\u7406&#xff0c;\u5e94\u8be5\u5c06\u9519\u8bef\u4fe1\u606f\u62a5\u544a\u7ed9\u8c03\u7528\u8005&#xff0c;\u8ba9\u8c03\u7528\u8005\u68c0\u67e5\u6587\u4ef6\u540d\u5b57\u662f\u5426\u7ed9\u9519\u4e86<br \/>\n    *\/<\/p>\n<p>    public void OpenConfig(String filename) throws FileNotFoundException{<br \/>\n        if(filename.equals(&#034;config.ini&#034;)){<br \/>\n            throw new FileNotFoundException(&#034;\u914d\u7f6e\u6587\u4ef6\u540d\u5b57\u4e0d\u5bf9&#034;);<br \/>\n        }<\/p>\n<p>        \/\/ \u6253\u5f00\u6587\u4ef6<br \/>\n    }<\/p>\n<p>    public void readConfig(){<br \/>\n    }<br \/>\n} <\/p>\n<p>\u3010\u6ce8\u610f\u4e8b\u9879\u3011<\/p>\n<li>\n<p>throws\u5fc5\u987b\u8ddf\u5728\u65b9\u6cd5\u7684\u53c2\u6570\u5217\u8868\u4e4b\u540e<\/p>\n<\/li>\n<li>\n<p>\u58f0\u660e\u7684\u5f02\u5e38\u5fc5\u987b&#xff08;\u5728\u5f02\u5e38\u7684\u8303\u7574\u91cc\u9762&#xff09;\u662f Exception \u6216\u8005 Exception \u7684\u5b50\u7c7b<\/p>\n<\/li>\n<li>\n<p>\u65b9\u6cd5\u5185\u90e8\u5982\u679c\u629b\u51fa\u4e86\u591a\u4e2a\u5f02\u5e38&#xff0c;throws\u4e4b\u540e\u5fc5\u987b\u8ddf\u591a\u4e2a\u5f02\u5e38\u7c7b\u578b&#xff0c;\u4e4b\u95f4\u7528\u9017\u53f7\u9694\u5f00&#xff0c;\u5982\u679c\u629b\u51fa\u591a\u4e2a\u5f02\u5e38\u7c7b\u578b\u5177\u6709\u7236\u5b50\u5173\u7cfb&#xff0c;\u76f4\u63a5\u58f0\u660e\u7236\u7c7b\u5373\u53ef\u3002<\/p>\n<\/li>\n<p> public class Config {<br \/>\n    File file;<\/p>\n<p>    \/\/ FileNotFoundException \u7ee7\u627f\u81ea IOException<br \/>\n    public void OpenConfig(String filename) throws IOException{<br \/>\n        if(!filename.endsWith(&#034;.ini&#034;)){<br \/>\n            throw new IOException(&#034;\u6587\u4ef6\u4e0d\u662f.ini\u6587\u4ef6&#034;);<br \/>\n        }<\/p>\n<p>        if(!filename.equals(&#034;config.ini&#034;)){<br \/>\n            throw new FileNotFoundException(&#034;\u914d\u7f6e\u6587\u4ef6\u540d\u5b57\u4e0d\u5bf9&#034;);<br \/>\n        }<\/p>\n<p>        \/\/ \u6253\u5f00\u6587\u4ef6<br \/>\n    }<\/p>\n<p>    public void readConfig(){<br \/>\n    }<br \/>\n} <\/p>\n<p>4. \u8c03\u7528\u58f0\u660e\u629b\u51fa\u5f02\u5e38\u7684\u65b9\u6cd5\u65f6&#xff0c;\u5982\u679c\u8be5\u5f02\u5e38\u662f\u7f16\u8bd1\u65f6\u5f02\u5e38\/\u53d7\u67e5\u5f02\u5e38\u65f6&#xff0c;\u8c03\u7528\u8005\u5fc5\u987b\u5bf9\u8be5\u5f02\u5e38\u8fdb\u884c\u5904\u7406&#xff0c;\u6216\u8005\u7ee7\u7eed\u4f7f\u7528throws\u629b\u51fa<\/p>\n<p> public static void main(String[] args) throws IOException {<br \/>\n    Config config &#061; new Config();<br \/>\n    config.openConfig(&#034;config.ini&#034;);<br \/>\n} <\/p>\n<p>\u5c06\u5149\u6807\u653e\u5728\u629b\u51fa\u5f02\u5e38\u65b9\u6cd5\u4e0a&#xff0c;alt &#043; Insert \u5feb\u901f \u5904\u7406&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"169\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260224104324-699d80cc2ec2f.png\" width=\"684\" \/><\/p>\n<h3 id=\"2.4%20%E5%BC%82%E5%B8%B8%E7%9A%84%E6%8D%95%E8%8E%B7-try-catch%E6%8D%95%E8%8E%B7%E5%B9%B6%E5%A4%84%E7%90%86%E5%BC%82%E5%B8%B8\">2.4 \u5f02\u5e38\u7684\u6355\u83b7-try-catch\u6355\u83b7\u5e76\u5904\u7406\u5f02\u5e38<\/h3>\n<p>throws\u5bf9\u5f02\u5e38\u5e76\u6ca1\u6709\u771f\u6b63\u5904\u7406&#xff0c;\u800c\u662f\u5c06\u5f02\u5e38\u62a5\u544a\u7ed9\u629b\u51fa\u5f02\u5e38\u65b9\u6cd5\u7684\u8c03\u7528\u8005&#xff0c;\u7531\u8c03\u7528\u8005\u5904\u7406\u3002\u5982\u679c\u771f\u6b63\u8981\u5bf9\u5f02\u5e38\u8fdb\u884c\u5904\u7406&#xff0c;\u5c31\u9700\u8981try-catch\u3002<\/p>\n<p>\u8bed\u6cd5\u683c\u5f0f&#xff1a;<\/p>\n<p>try{<br \/>\n    \/\/ \u5c06\u53ef\u80fd\u51fa\u73b0\u5f02\u5e38\u7684\u4ee3\u7801\u653e\u5728\u8fd9\u91cc<br \/>\n}catch(\u8981\u6355\u83b7\u7684\u5f02\u5e38\u7c7b\u578b e){<br \/>\n    \/\/ \u5982\u679ctry\u4e2d\u7684\u4ee3\u7801\u629b\u51fa\u5f02\u5e38\u4e86&#xff0c;\u6b64\u5904catch\u6355\u83b7\u65f6\u5f02\u5e38\u7c7b\u578b\u4e0etry\u4e2d\u629b\u51fa\u7684\u5f02\u5e38\u7c7b\u578b\u4e00\u81f4<br \/>\n    \/\/ \u65f6&#xff0c;\u6216\u8005\u662ftry\u4e2d\u629b\u51fa\u5f02\u5e38\u7684\u57fa\u7c7b\u65f6&#xff0c;\u5c31\u4f1a\u88ab\u6355\u83b7\u5230<br \/>\n    \/\/ \u5bf9\u5f02\u5e38\u5c31\u53ef\u4ee5\u6b63\u5e38\u5904\u7406&#xff0c;\u5904\u7406\u5b8c\u6210\u540e&#xff0c;\u8df3\u51fatry-catch\u7ed3\u6784&#xff0c;\u7ee7\u7eed\u6267\u884c\u540e\u7eed\u4ee3\u7801<br \/>\n}catch(\u5f02\u5e38\u7c7b\u578b e){<br \/>\n    \/\/ \u5bf9\u5f02\u5e38\u8fdb\u884c\u5904\u7406<br \/>\n}finally{<br \/>\n    \/\/ \u6b64\u5904\u4ee3\u7801\u4e00\u5b9a\u4f1a\u88ab\u6267\u884c\u5230<br \/>\n}<\/p>\n<p>\/\/ \u540e\u5e8f\u4ee3\u7801<br \/>\n\/\/ \u5f53\u5f02\u5e38\u88ab\u6355\u83b7\u5230\u65f6&#xff0c;\u5f02\u5e38\u5c31\u88ab\u5904\u7406\u4e86&#xff0c;\u8fd9\u91cc\u7684\u540e\u5e8f\u4ee3\u7801\u4e00\u5b9a\u4f1a\u6267\u884c<br \/>\n\/\/ \u5982\u679c\u6355\u83b7\u4e86&#xff0c;\u7531\u4e8e\u6355\u83b7\u5230\u7c7b\u578b\u4e0d\u5bf9&#xff0c;\u90a3\u5c31\u6ca1\u6709\u6355\u83b7\u5230&#xff0c;\u8fd9\u91cc\u7684\u4ee3\u7801\u5c31\u4e0d\u4f1a\u88ab\u6267\u884c <\/p>\n<p>\u6ce8\u610f&#xff1a;<\/p>\n<li>\n<p>[]\u4e2d\u8868\u793a\u53ef\u9009\u9879&#xff0c;\u53ef\u4ee5\u6dfb\u52a0&#xff0c;\u4e5f\u53ef\u4ee5\u4e0d\u7528\u6dfb\u52a0<\/p>\n<\/li>\n<li>\n<p>try\u4e2d\u7684\u4ee3\u7801\u53ef\u80fd\u4f1a\u629b\u51fa\u5f02\u5e38&#xff0c;\u4e5f\u53ef\u80fd\u4e0d\u4f1a<\/p>\n<\/li>\n<p>\u9700\u6c42&#xff1a;\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6&#xff0c;\u5982\u679c\u914d\u7f6e\u6587\u4ef6\u540d\u5b57\u4e0d\u662f\u6307\u5b9a\u540d\u5b57&#xff0c;\u629b\u51fa\u5f02\u5e38&#xff0c;\u8c03\u7528\u8005\u8fdb\u884c\u5f02\u5e38\u5904\u7406<\/p>\n<p>public class Config {<br \/>\n    File file;<\/p>\n<p>    public void openConfig(String filename) throws FileNotFoundException{<br \/>\n        if(!filename.equals(&#034;config.ini&#034;)){<br \/>\n            throw new FileNotFoundException(&#034;\u914d\u7f6e\u6587\u4ef6\u540d\u5b57\u4e0d\u5bf9&#034;);<br \/>\n        }<\/p>\n<p>        \/\/ \u6253\u5f00\u6587\u4ef6<br \/>\n    }<\/p>\n<p>    public void readConfig(){<br \/>\n    }<br \/>\n}<\/p>\n<p>public static void main(String[] args) {<br \/>\n    Config config &#061; new Config();<br \/>\n    try {<br \/>\n        config.openConfig(&#034;config.txt&#034;);<br \/>\n        System.out.println(&#034;\u6587\u4ef6\u6253\u5f00\u6210\u529f&#034;);<br \/>\n    } catch (IOException e) {<br \/>\n        \/\/ \u5f02\u5e38\u7684\u5904\u7406\u65b9\u5f0f<br \/>\n        \/\/System.out.println(e.getMessage());<br \/>\n        \/\/System.out.println(e);<br \/>\n        e.printStackTrace();<br \/>\n    }<\/p>\n<p>    \/\/ \u4e00\u65e6\u5f02\u5e38\u88ab\u6355\u83b7\u5904\u7406\u4e86&#xff0c;\u6b64\u5904\u7684\u4ee3\u7801\u4f1a\u6267\u884c<br \/>\n    System.out.println(&#034;\u5f02\u5e38\u5982\u679c\u88ab\u5904\u7406\u4e86&#xff0c;\u8fd9\u91cc\u7684\u4ee3\u7801\u4e5f\u53ef\u4ee5\u6267\u884c&#034;);<br \/>\n} <\/p>\n<h4 id=\"%E3%80%90%E5%85%B3%E4%BA%8E%E5%BC%82%E5%B8%B8%E7%9A%84%E5%A4%84%E7%90%86%E6%96%B9%E5%BC%8F%E3%80%911%E3%80%812%E3%80%813\"><span style=\"color:#be191c\">\u3010\u5173\u4e8e\u5f02\u5e38\u7684\u5904\u7406\u65b9\u5f0f\u30111\u30012\u30013<\/span><\/h4>\n<p>\u5f02\u5e38\u7684\u79cd\u7c7b\u6709\u5f88\u591a&#xff0c;\u6211\u4eec\u8981\u6839\u636e\u4e0d\u540c\u7684\u4e1a\u52a1\u573a\u666f\u6765\u51b3\u5b9a\u3002<\/p>\n<li>\n<p>\u5bf9\u4e8e\u6bd4\u8f83\u4e25\u91cd\u7684\u95ee\u9898&#xff08;\u4f8b\u5982\u548c\u7b97\u94b1\u76f8\u5173\u7684\u573a\u666f&#xff09;&#xff0c;\u5e94\u8be5\u8ba9\u7a0b\u5e8f\u76f4\u63a5\u5d29\u6e83&#xff0c;\u9632\u6b62\u9020\u6210\u66f4\u4e25\u91cd\u7684\u540e\u679c<\/p>\n<\/li>\n<li>\n<p>\u5bf9\u4e8e\u4e0d\u592a\u4e25\u91cd\u7684\u95ee\u9898&#xff08;\u5927\u591a\u6570\u573a\u666f&#xff09;&#xff0c;\u53ef\u4ee5\u8bb0\u5f55\u9519\u8bef\u65e5\u5fd7&#xff0c;\u5e76\u901a\u8fc7\u76d1\u63a7\u62a5\u8b66\u7a0b\u5e8f\u53ca\u65f6\u901a\u77e5\u7a0b\u5e8f\u733f<\/p>\n<\/li>\n<li>\n<p>\u5bf9\u4e8e\u53ef\u80fd\u4f1a\u6062\u590d\u7684\u95ee\u9898&#xff08;\u548c\u7f51\u7edc\u76f8\u5173\u7684\u573a\u666f&#xff09;&#xff0c;\u53ef\u4ee5\u5c1d\u8bd5\u8fdb\u884c\u91cd\u8bd5\u3002<\/p>\n<\/li>\n<p>\u5728\u6211\u4eec\u5f53\u524d\u7684\u4ee3\u7801\u4e2d\u91c7\u53d6\u7684\u662f\u7ecf\u8fc7\u7b80\u5316\u7684\u7b2c\u4e8c\u79cd\u65b9\u5f0f\u3002\u6211\u4eec\u8bb0\u5f55\u7684\u9519\u8bef\u65e5\u5fd7\u662f\u51fa\u73b0\u5f02\u5e38\u7684\u65b9\u6cd5\u8c03\u7528\u4fe1\u606f&#xff0c;\u80fd\u5f88\u5feb\u901f\u7684\u8ba9\u6211\u4eec\u627e\u5230\u51fa\u73b0\u5f02\u5e38\u7684\u4f4d\u7f6e\u3002\u4ee5\u540e\u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\u6211\u4eec\u4f1a\u91c7\u53d6\u66f4\u5b8c\u5907\u7684\u65b9\u5f0f\u6765\u8bb0\u5f55\u5f02\u5e38\u4fe1\u606f\u3002<\/p>\n<h4 id=\"%E3%80%90%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9%E3%80%911%E3%80%812%E3%80%813%E3%80%814%E3%80%815\"><span style=\"color:#be191c\">\u3010\u6ce8\u610f\u4e8b\u9879\u30111\u30012\u30013\u30014\u30015<\/span><\/h4>\n<p>1. try\u5757\u5185\u629b\u51fa\u5f02\u5e38\u4f4d\u7f6e\u4e4b\u540e\u7684\u4ee3\u7801\u5c06\u4e0d\u4f1a\u88ab\u6267\u884c<\/p>\n<p>2. \u5982\u679c\u629b\u51fa\u5f02\u5e38\u7c7b\u578b\u4e0ecatch\u4e2d\u5f02\u5e38\u7c7b\u578b\u4e0d\u5339\u914d&#xff0c;\u5373\u5f02\u5e38\u4e0d\u4f1a\u88ab\u6210\u529f\u6355\u83b7&#xff0c;\u4e5f\u5c31\u4e0d\u4f1a\u88ab\u5904\u7406&#xff0c;\u7ee7\u7eed\u5f80\u5916\u629b&#xff0c;\u76f4\u5230JVM\u6536\u5230\u540e\u4e2d\u65ad\u7a0b\u5e8f&#8212;-\u5f02\u5e38\u662f\u6309\u7167\u7c7b\u578b\u6765\u6355\u83b7\u7684<\/p>\n<p> public class Test2 {<\/p>\n<p>    public static void func2(int a) throws ArithmeticException{<br \/>\n        if(a &#061;&#061; 0) {<br \/>\n22            throw new ArithmeticException();<br \/>\n        }<br \/>\n        System.out.println(10\/a);<br \/>\n    }<\/p>\n<p>    public static void main(String[] args) {<br \/>\n        try {<br \/>\n            func2(a: 0); \/\/ \u53ef\u80fd\u53d1\u751f\u5f02\u5e38\u7684\u4ee3\u7801<br \/>\n        }catch (ArithmeticException e) { \/\/ \u6355\u83b7\u7684 try\u5f53\u4e2d \u53ef\u80fd\u53d1\u751f\u7684\u5f02\u5e38<br \/>\n            e.printStackTrace();<br \/>\n            System.out.println(&#034;\u6355\u6349\u5230\u4e86\u7b97\u672f\u5f02\u5e38&#xff0c;\u903b\u8f91 . . . .&#034;);<br \/>\n        }<\/p>\n<p>        System.out.println(&#034;haha!&#034;);<br \/>\n    }<br \/>\n} <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"208\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260224104324-699d80cc43a4f.png\" width=\"499\" \/><\/p>\n<p>public static void main(String[] args) {<br \/>\n    try {<br \/>\n        int[] array &#061; {1, 2, 3};<br \/>\n        System.out.println(array[3]); \/\/ \u6b64\u5904\u4f1a\u629b\u51fa\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38<br \/>\n    } catch (NullPointerException e) { \/\/ \u6355\u83b7\u65f6\u5019\u6355\u83b7\u7684\u662f\u7a7a\u6307\u9488\u5f02\u5e38&#8211;\u771f\u6b63\u7684\u5f02\u5e38\u65e0\u6cd5\u88ab\u6355\u83b7\u5230<br \/>\n        e.printStackTrace();<br \/>\n    }<\/p>\n<p>    System.out.println(&#034;\u540e\u7eed\u4ee3\u7801&#034;);<br \/>\n}<br \/>\nException in thread &#034;main&#034; java.lang.ArrayIndexOutOfBoundsException: 3<\/p>\n<p>at day20210917.ArrayOperator.main(ArrayOperator.java:24) <\/p>\n<p>3. try\u4e2d\u53ef\u80fd\u4f1a\u629b\u51fa\u591a\u4e2a\u4e0d\u540c\u7684\u5f02\u5e38\u5bf9\u8c61&#xff0c;\u5219\u5fc5\u987b\u7528\u591a\u4e2acatch\u6765\u6355\u83b7&#8212;-\u5373\u591a\u79cd\u5f02\u5e38&#xff0c;\u591a\u6b21\u6355\u83b7<\/p>\n<p>public static void main(String[] args) {<br \/>\n    int[] array &#061; {1, 2, 3};<\/p>\n<p>    try {<br \/>\n        System.out.println(&#034;before&#034;);<br \/>\n        \/\/ array &#061; null;<br \/>\n        System.out.println(array[100]);<br \/>\n        System.out.println(&#034;after&#034;);<br \/>\n    } catch (ArrayIndexOutOfBoundsException e) {<br \/>\n        System.out.println(&#034;\u8fd9\u662f\u4e2a\u6570\u7ec4\u4e0b\u6807\u8d8a\u754c\u5f02\u5e38&#034;);<br \/>\n        e.printStackTrace();<br \/>\n    } catch (NullPointerException e) {<br \/>\n        System.out.println(&#034;\u8fd9\u662f\u4e2a\u7a7a\u6307\u9488\u5f02\u5e38&#034;);<br \/>\n        e.printStackTrace();<br \/>\n    }<br \/>\n    System.out.println(&#034;after try catch&#034;);<br \/>\n} <\/p>\n<p>\u5982\u679c\u591a\u4e2a\u5f02\u5e38\u7684\u5904\u7406\u65b9\u5f0f\u662f\u5b8c\u5168\u76f8\u540c&#xff0c;\u4e5f\u53ef\u4ee5\u5199\u6210\u8fd9\u6837&#xff08;\u8bed\u6cd5\u652f\u6301\u4f46\u4e0d\u5efa\u8bae&#xff09;&#xff1a;<\/p>\n<p>catch (ArrayIndexOutOfBoundsException | NullPointerException e) {<br \/>\n    &#8230;<br \/>\n} <\/p>\n<p>\u5982\u679c\u5f02\u5e38\u4e4b\u95f4\u5177\u6709\u7236\u5b50\u5173\u7cfb&#xff0c;\u4e00\u5b9a\u662f\u5b50\u7c7b\u5f02\u5e38\u5728\u524dcatch&#xff0c;\u7236\u7c7b\u5f02\u5e38\u5728\u540ecatch&#xff0c;\u5426\u5219\u8bed\u6cd5\u9519\u8bef&#xff1a;<\/p>\n<p>public static void main(String[] args) {<br \/>\n    int[] arr &#061; {1, 2, 3};<br \/>\n    try {<br \/>\n        System.out.println(&#034;before&#034;);<br \/>\n        arr &#061; null;<br \/>\n        System.out.println(arr[100]);<br \/>\n        System.out.println(&#034;after&#034;);<br \/>\n    } catch (Exception e) { \/\/ Exception\u53ef\u4ee5\u6355\u83b7\u5230\u6240\u6709\u5f02\u5e38<br \/>\n        e.printStackTrace();<br \/>\n    } catch (NullPointerException e) { \/\/ \u6c38\u8fdc\u90fd\u6355\u83b7\u6267\u884c\u5230<br \/>\n        e.printStackTrace();<br \/>\n    }<\/p>\n<p>    System.out.println(&#034;after try catch&#034;);<br \/>\n}<br \/>\nError:(33, 10) java: \u5df2\u6355\u83b7\u5230\u5f02\u5e38\u9519\u8befjava.lang.NullPointerException <\/p>\n<p>4. \u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2acatch\u6355\u83b7\u6240\u6709\u7684\u5f02\u5e38&#xff0c;\u5373\u591a\u4e2a\u5f02\u5e38&#xff0c;\u4e00\u6b21\u6355\u83b7(\u4e0d\u63a8\u8350)<\/p>\n<p>public static void main(String[] args) {<br \/>\n    int[] arr &#061; {1, 2, 3};<br \/>\n    try {<br \/>\n        System.out.println(&#034;before&#034;);<br \/>\n        arr &#061; null;<br \/>\n        System.out.println(arr[100]);<br \/>\n        System.out.println(&#034;after&#034;);<br \/>\n    } catch (Exception e) {<br \/>\n        e.printStackTrace();<br \/>\n    }<br \/>\n    System.out.println(&#034;after try catch&#034;);<br \/>\n} <\/p>\n<p>\u7531\u4e8e Exception \u7c7b\u662f\u6240\u6709\u5f02\u5e38\u7c7b\u7684\u7236\u7c7b&#xff0c;\u56e0\u6b64\u53ef\u4ee5\u7528\u8fd9\u4e2a\u7c7b\u578b\u8868\u793a\u6355\u6349\u6240\u6709\u5f02\u5e38\u3002<\/p>\n<p>\u5907\u6ce8&#xff1a;catch \u8fdb\u884c\u7c7b\u578b\u5339\u914d\u7684\u65f6\u5019&#xff0c;\u4e0d\u5149\u4f1a\u5339\u914d\u76f8\u540c\u7c7b\u578b\u7684\u5f02\u5e38\u5bf9\u8c61&#xff0c;\u4e5f\u4f1a\u6355\u6349\u76ee\u6807\u5f02\u5e38\u7c7b\u578b\u7684\u5b50\u7c7b\u5bf9\u8c61\u3002<\/p>\n<p>\u5982\u521a\u624d\u7684\u4ee3\u7801&#xff0c;NullPointerException \u548c ArrayIndexOutOfBoundsException \u90fd\u662f Exception \u7684\u5b50\u7c7b&#xff0c;\u56e0\u6b64\u90fd\u80fd\u88ab\u6355\u83b7\u5230\u3002<\/p>\n<p>5. \u7236\u7c7b\u5f02\u5e38&#xff08;\u5bb6\u957f&#xff09;\u4f5c\u4e3a\u540e\u76fe&#8211;\u653e\u5728\u540e\u9762<\/p>\n<p>public class Test3 {<\/p>\n<p>    public static void main(String[] args) {<br \/>\n        int[] array &#061; {1, 2, 3};<\/p>\n<p>        try {<br \/>\n            System.out.println(&#034;before&#034;);<br \/>\n            array &#061; null;<br \/>\n            \/\/System.out.println(array.length);<br \/>\n            System.out.println(array[100]);<br \/>\n            System.out.println(&#034;after&#034;);<br \/>\n        } catch (ArrayIndexOutOfBoundsException e) {<br \/>\n            System.out.println(&#034;\u8fd9\u662f\u4e2a\u6570\u7ec4\u4e0b\u6807\u8d8a\u754c\u5f02\u5e38&#034;);<br \/>\n            e.printStackTrace();<br \/>\n        } catch (NullPointerException e) {<br \/>\n            System.out.println(&#034;\u8fd9\u662f\u4e2a\u7a7a\u6307\u9488\u5f02\u5e38&#034;);<br \/>\n            e.printStackTrace();<br \/>\n        } catch (RuntimeException e) {<br \/>\n            e.printStackTrace();<br \/>\n        }<br \/>\n        System.out.println(&#034;after try catch&#034;);<br \/>\n    }<br \/>\n} <\/p>\n<h3 id=\"2.5%20finally\">2.5 finally<\/h3>\n<p>\u5728\u5199\u7a0b\u5e8f\u65f6&#xff0c;\u6709\u4e9b\u7279\u5b9a\u7684\u4ee3\u7801&#xff0c;\u4e0d\u8bba\u7a0b\u5e8f\u662f\u5426\u53d1\u751f\u5f02\u5e38&#xff0c;\u90fd\u9700\u8981\u6267\u884c&#xff0c;\u6bd4\u5982\u7a0b\u5e8f\u4e2d\u6253\u5f00\u7684\u8d44\u6e90&#xff1a;\u7f51\u7edc\u8fde\u63a5\u3001\u6570\u636e\u5e93\u8fde\u63a5\u3001IO\u6d41\u7b49&#xff0c;\u5728\u7a0b\u5e8f\u6b63\u5e38\u6216\u8005\u5f02\u5e38\u9000\u51fa\u65f6&#xff0c;\u5fc5\u987b\u8981\u5bf9\u8d44\u6e90\u8fdb\u884c\u56de\u6536\u3002\u53e6\u5916&#xff0c;\u56e0\u4e3a\u5f02\u5e38\u4f1a\u5f15\u53d1\u7a0b\u5e8f\u7684\u8df3\u8f6c&#xff0c;\u53ef\u80fd\u5bfc\u81f4\u6709\u4e9b\u8bed\u53e5\u6267\u884c\u4e0d\u5230&#xff0c;finally\u5c31\u662f\u7528\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u3002<\/p>\n<p>\u8bed\u6cd5\u683c\u5f0f&#xff1a;<\/p>\n<p>try{<br \/>\n    \/\/ \u53ef\u80fd\u4f1a\u53d1\u751f\u5f02\u5e38\u7684\u4ee3\u7801<br \/>\n}catch(\u5f02\u5e38\u7c7b\u578b e){<br \/>\n    \/\/ \u5bf9\u6355\u83b7\u5230\u7684\u5f02\u5e38\u8fdb\u884c\u5904\u7406<br \/>\n}finally{<br \/>\n    \/\/ \u6b64\u5904\u7684\u8bed\u53e5\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38&#xff0c;\u90fd\u4f1a\u88ab\u6267\u884c\u5230<br \/>\n}<\/p>\n<p>\/\/ \u5982\u679c\u6ca1\u6709\u629b\u51fa\u5f02\u5e38&#xff0c;\u6216\u8005\u5f02\u5e38\u88ab\u6355\u83b7\u5904\u7406\u4e86&#xff0c;\u8fd9\u91cc\u7684\u4ee3\u7801\u4e5f\u4f1a\u6267\u884c<br \/>\npublic static void main(String[] args) {<br \/>\n    try{<br \/>\n        int[] array &#061; {1, 2, 3};<br \/>\n        array [100] &#061; 10;<br \/>\n        array [0] &#061; 10;<br \/>\n    }catch (ArrayIndexOutOfBoundsException e){<br \/>\n        System.out.println(e);<br \/>\n    }finally {<br \/>\n        System.out.println(&#034;Finally\u4e2d\u7684\u4ee3\u7801\u4e00\u5b9a\u4f1a\u6267\u884c&#034;);<br \/>\n    }<\/p>\n<p>    System.out.println(&#034;\u5982\u679c\u6ca1\u6709\u629b\u51fa\u5f02\u5e38&#xff0c;\u6216\u8005\u5f02\u5e38\u88ab\u5904\u7406\u4e86&#xff0c;try-catch\u540e\u7684\u4ee3\u7801\u4e5f\u4f1a\u6267\u884c&#034;);<br \/>\n} <\/p>\n<p>\u95ee\u9898&#xff1a;\u65e2\u7136 finally \u548c try-catch-finally \u540e\u7684\u4ee3\u7801\u90fd\u4f1a\u6267\u884c&#xff0c;\u90a3\u4e3a\u4ec0\u4e48\u8fd8\u8981\u6709 finally \u5462&#xff1f;<\/p>\n<p>\u9700\u6c42&#xff1a;\u5b9e\u73b0getData\u65b9\u6cd5&#xff0c;\u5185\u90e8\u8f93\u5165\u4e00\u4e2a\u6574\u578b\u6570\u5b57&#xff0c;\u7136\u540e\u5c06\u8be5\u6570\u5b57\u8fd4\u56de&#xff0c;\u5e76\u518dmain\u65b9\u6cd5\u4e2d\u6253\u5370\u3002<\/p>\n<p> public class TestFinally {<br \/>\n    public static int getData(){<br \/>\n        Scanner sc &#061; null;<br \/>\n        try{<br \/>\n            sc &#061; new Scanner(System.in);<br \/>\n            int data &#061; sc.nextInt();<br \/>\n            return data;<br \/>\n        }catch (InputMismatchException e){<br \/>\n            e.printStackTrace();<br \/>\n        }finally {<br \/>\n            System.out.println(&#034;finally\u4e2d\u4ee3\u7801&#034;);<br \/>\n        }<\/p>\n<p>        System.out.println(&#034;try-catch-finally\u4e4b\u540e\u4ee3\u7801&#034;);<br \/>\n        if(null !&#061; sc){<br \/>\n            sc.close();<br \/>\n        }<\/p>\n<p>        return 0;<br \/>\n    }<\/p>\n<p>    public static void main(String[] args) {<br \/>\n        int data &#061; getData();<br \/>\n        System.out.println(data);<br \/>\n    }<br \/>\n} <\/p>\n<p>\/\/ \u6b63\u5e38\u8f93\u5165\u65f6\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c&#xff1a;<\/p>\n<p>100<\/p>\n<p>finally\u4e2d\u4ee3\u7801<\/p>\n<p>100<\/p>\n<p><span style=\"color:#be191c\">\u6b64\u5916&#xff0c;\u4e0a\u8ff0\u4ee3\u7801\u7b49\u4ef7\u4e8e\u4e0b\u9762\u4ee3\u7801&#xff08;\u66f4\u7b80\u4fbf&#xff0c;\u4e14\u4e0d\u7528\u5199close\u4f46idea\u4e5f\u4f1a\u81ea\u52a8\u56de\u6536\u8d44\u6e90&#xff09;&#xff1a;<\/span><\/p>\n<p> public static int getData(){<br \/>\n    try (Scanner sc &#061; new Scanner(System.in)) {<br \/>\n        int data &#061; sc.nextInt();<br \/>\n        return data;<br \/>\n    } catch (InputMismatchException e) {<br \/>\n        e.printStackTrace();<br \/>\n    } finally {<br \/>\n        System.out.println(&#034;finally\u4e2d\u4ee3\u7801&#034;);<br \/>\n    }<br \/>\n    System.out.println(&#034;try-catch-finally\u4e4b\u540e\u4ee3\u7801&#034;);<br \/>\n    return 0;<br \/>\n} <\/p>\n<p>\u4e0a\u8ff0\u7a0b\u5e8f&#xff0c;\u5982\u679c\u6b63\u5e38\u8f93\u5165&#xff0c;\u6210\u529f\u63a5\u6536\u8f93\u5165\u540e\u7a0b\u5e8f\u5c31\u8fd4\u56de\u4e86&#xff0c;try-catch-finally\u4e4b\u540e\u7684\u4ee3\u7801\u6839\u672c\u5c31\u6ca1\u6709\u6267\u884c&#xff0c;\u5373\u8f93\u5165\u6d41\u5c31\u6ca1\u6709\u88ab\u91ca\u653e&#xff0c;\u9020\u6210\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<p>\u6ce8\u610f&#xff1a;finally\u4e2d\u7684\u4ee3\u7801\u4e00\u5b9a\u4f1a\u6267\u884c\u7684&#xff0c;\u4e00\u822c\u5728finally\u4e2d\u8fdb\u884c\u4e00\u4e9b\u8d44\u6e90\u6e05\u7406\u7684\u626b\u5c3e\u5de5\u4f5c\u3002<\/p>\n<p>\/\/ \u4e0b\u9762\u7a0b\u5e8f\u8f93\u51fa\u4ec0\u4e48&#xff1f;<\/p>\n<p> public static void main(String[] args) {<br \/>\n    System.out.println(func());<br \/>\n}<\/p>\n<p>public static int func() {<br \/>\n    try {<br \/>\n        return 10;<br \/>\n    } finally {<br \/>\n        return 20;<br \/>\n    }<br \/>\n} <\/p>\n<p>A: 10 B: 20 C: 30 D: \u7f16\u8bd1\u5931\u8d25<\/p>\n<p><span style=\"color:#be191c\">\u7b54\u6848&#xff1a;B<\/span><\/p>\n<p>finally \u6267\u884c\u7684\u65f6\u673a\u662f\u5728\u65b9\u6cd5\u8fd4\u56de\u4e4b\u524d(try \u6216\u8005 catch \u4e2d\u5982\u679c\u6709 return \u4f1a\u5728\u8fd9\u4e2a return \u4e4b\u524d\u6267\u884c finally)\u3002\u4f46\u662f\u5982\u679c finally \u4e2d\u4e5f\u5b58\u5728 return \u8bed\u53e5&#xff0c;\u90a3\u4e48\u5c31\u4f1a\u6267\u884c finally \u4e2d\u7684 return&#xff0c;\u4ece\u800c\u4e0d\u4f1a\u6267\u884c\u5230 try \u4e2d\u539f\u6709\u7684 return\u3002<\/p>\n<p>\u4e00\u822c\u6211\u4eec\u4e0d\u5efa\u8bae\u5728 finally \u4e2d\u5199 return (\u88ab\u7f16\u8bd1\u5668\u5f53\u505a\u4e00\u4e2a\u8b66\u544a)\u3002<\/p>\n<h3 id=\"2.6%20%E5%BC%82%E5%B8%B8%E7%9A%84%E5%A4%84%E7%90%86%E6%B5%81%E7%A8%8B\">2.6 \u5f02\u5e38\u7684\u5904\u7406\u6d41\u7a0b<\/h3>\n<p>\u5173\u4e8e\u201c\u8c03\u7528\u6808\u201d<\/p>\n<p>\u65b9\u6cd5\u4e4b\u95f4\u662f\u5b58\u5728\u76f8\u4e92\u8c03\u7528\u5173\u7cfb\u7684&#xff0c;\u8fd9\u79cd\u8c03\u7528\u5173\u7cfb\u6211\u4eec\u53ef\u4ee5\u7528\u201c\u8c03\u7528\u6808\u201d\u6765\u63cf\u8ff0\u3002\u5728 JVM \u4e2d\u6709\u4e00\u5757\u5185\u5b58\u7a7a\u95f4\u79f0\u4e3a\u201c\u865a\u62df\u673a\u6808\u201d\u4e13\u95e8\u5b58\u50a8\u65b9\u6cd5\u4e4b\u95f4\u7684\u8c03\u7528\u5173\u7cfb\u3002\u5f53\u4ee3\u7801\u4e2d\u51fa\u73b0\u5f02\u5e38\u7684\u65f6\u5019&#xff0c;\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528 e.printStackTrace();\u7684\u65b9\u5f0f\u67e5\u770b\u51fa\u73b0\u5f02\u5e38\u4ee3\u7801\u7684\u8c03\u7528\u6808\u3002<\/p>\n<p>\u5982\u679c\u672c\u65b9\u6cd5\u4e2d\u6ca1\u6709\u5408\u9002\u7684\u5904\u7406\u5f02\u5e38\u7684\u65b9\u5f0f&#xff0c;\u5c31\u4f1a\u6cbf\u7740\u8c03\u7528\u6808\u5411\u4e0a\u4f20\u9012\u3002<\/p>\n<p>public static void main(String[] args) {<br \/>\n    try {<br \/>\n        func();<br \/>\n    } catch (ArrayIndexOutOfBoundsException e) {<br \/>\n        e.printStackTrace();<br \/>\n    }<br \/>\n    System.out.println(&#034;after try catch&#034;);<br \/>\n}<\/p>\n<p>public static void func() {<br \/>\n    int[] array &#061; {1, 2, 3};<br \/>\n    System.out.println(array[100]);<br \/>\n} <\/p>\n<p>\u8fd0\u884c\u7ed3\u679c&#xff1a;<\/p>\n<p>java.lang.ArrayIndexOutOfBoundsException: Index 100 out of bounds for length 3<br \/>\n    at LibrarySystem.func(LibrarySystem.java:107)<br \/>\n    at LibrarySystem.main(LibrarySystem.java:98)<br \/>\nafter try catch <\/p>\n<p>\u5982\u679c\u5411\u4e0a\u4f20\u9012\u90fd\u6ca1\u6709\u5408\u9002\u7684\u65b9\u6cd5\u5904\u7406\u5f02\u5e38&#xff0c;\u6700\u7ec8\u5c31\u4f1a\u4ea4\u7ed9 JVM \u5904\u7406&#xff0c;\u7a0b\u5e8f\u5c31\u4f1a\u5f02\u5e38\u7ec8\u6b62&#xff08;\u548c\u6211\u4eec\u6700\u5f00\u59cb\u672a\u4f7f\u7528 try catch \u65f6\u662f\u4e00\u6837\u7684&#xff09;\u3002<\/p>\n<p>public static void main(String[] args) {<br \/>\n    func();<br \/>\n    System.out.println(&#034;after try catch&#034;);<br \/>\n}<\/p>\n<p>public static void func() {<br \/>\n    int[] arr &#061; {1, 2, 3};<br \/>\n    System.out.println(arr[100]);<br \/>\n} <\/p>\n<p>\u8fd0\u884c\u7ed3\u679c&#xff1a;<\/p>\n<p>Exception in thread &#034;main&#034; java.lang.ArrayIndexOutOfBoundsException: Index 100 out of bounds for length 3<br \/>\n    at LibrarySystem.func(LibrarySystem.java:103)<br \/>\n    at LibrarySystem.main(LibrarySystem.java:97) <\/p>\n<p>\u5f88\u660e\u663e&#xff1a;<span style=\"color:#be191c\">after try catch<\/span> \u5e76\u6ca1\u6709\u88ab\u6253\u5370<\/p>\n<p>\u3010\u5f02\u5e38\u5904\u7406\u6d41\u7a0b\u603b\u7ed3\u3011<\/p>\n<ul>\n<li>\n<p>\u7a0b\u5e8f\u5148\u6267\u884c try \u4e2d\u7684\u4ee3\u7801<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c try \u4e2d\u7684\u4ee3\u7801\u51fa\u73b0\u5f02\u5e38&#xff0c;\u5c31\u4f1a\u7ed3\u675f try \u4e2d\u7684\u4ee3\u7801&#xff0c;\u770b\u548c catch \u4e2d\u7684\u5f02\u5e38\u7c7b\u578b\u662f\u5426\u5339\u914d\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u627e\u5230\u5339\u914d\u7684\u5f02\u5e38\u7c7b\u578b&#xff0c;\u5c31\u4f1a\u6267\u884c catch \u4e2d\u7684\u4ee3\u7801<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u5f02\u5e38\u7c7b\u578b&#xff0c;\u5c31\u4f1a\u5c06\u5f02\u5e38\u5411\u4e0a\u4f20\u9012\u5230\u4e0a\u5c42\u8c03\u7528\u8005\u3002<\/p>\n<\/li>\n<li>\n<p>\u65e0\u8bba\u662f\u5426\u627e\u5230\u5339\u914d\u7684\u5f02\u5e38\u7c7b\u578b&#xff0c;finally \u4e2d\u7684\u4ee3\u7801\u90fd\u4f1a\u88ab\u6267\u884c\u5230&#xff08;\u5728\u8be5\u65b9\u6cd5\u7ed3\u675f\u4e4b\u524d\u6267\u884c&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u4e0a\u5c42\u8c03\u7528\u8005\u4e5f\u6ca1\u6709\u5904\u7406\u4e86\u7684\u5f02\u5e38&#xff0c;\u5c31\u7ee7\u7eed\u5411\u4e0a\u4f20\u9012\u3002<\/p>\n<\/li>\n<li>\n<p>\u4e00\u76f4\u5230 main \u65b9\u6cd5\u4e5f\u6ca1\u6709\u5408\u9002\u7684\u4ee3\u7801\u5904\u7406\u5f02\u5e38&#xff0c;\u5c31\u4f1a\u4ea4\u7ed9 JVM \u6765\u8fdb\u884c\u5904\u7406&#xff0c;\u6b64\u65f6\u7a0b\u5e8f\u5c31\u4f1a\u5f02\u5e38\u7ec8\u6b62\u3002<\/p>\n<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"260\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260224104324-699d80cc5bd78.png\" width=\"441\" \/><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"115\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260224104324-699d80cc74229.png\" width=\"831\" \/><\/p>\n<hr \/>\n<h2 id=\"3.%20%E8%87%AA%E5%AE%9A%E4%B9%89%E5%BC%82%E5%B8%B8\">3. \u81ea\u5b9a\u4e49\u5f02\u5e38<\/h2>\n<p>Java \u4e2d\u867d\u7136\u5df2\u7ecf\u5185\u7f6e\u4e86\u4e30\u5bcc\u7684\u5f02\u5e38\u7c7b&#xff0c;\u4f46\u662f\u5e76\u4e0d\u80fd\u5b8c\u5168\u8868\u793a\u5b9e\u9645\u5f00\u53d1\u4e2d\u6240\u9047\u5230\u7684\u4e00\u4e9b\u5f02\u5e38&#xff0c;\u6b64\u65f6\u5c31\u9700\u8981\u7ef4\u62a4\u7b26\u5408\u6211\u4eec\u5b9e\u9645\u60c5\u51b5\u7684\u5f02\u5e38\u7ed3\u6784\u3002<\/p>\n<p>\u4f8b\u5982&#xff0c;\u6211\u4eec\u5b9e\u73b0\u4e00\u4e2a\u7528\u6237\u767b\u9646\u529f\u80fd\u3002<\/p>\n<p>public class Login {<br \/>\n    private String userName &#061; &#034;admin&#034;;<br \/>\n    private String password &#061; &#034;123456&#034;;<br \/>\n    public void loginInfo(String userName, String password) {<br \/>\n        if (!this.userName.equals(userName)) {<br \/>\n            System.out.println(&#034;\u7528\u6237\u540d\u9519\u8bef&#xff01;&#034;);<br \/>\n            return;<br \/>\n        }<\/p>\n<p>        if (!this.password.equals(password)) {<br \/>\n            System.out.println(&#034;\u5bc6\u7801\u9519\u8bef&#xff01;&#034;);<br \/>\n            return;<br \/>\n        }<br \/>\n        System.out.println(&#034;\u767b\u9646\u6210\u529f&#034;);<br \/>\n    }<\/p>\n<p>    public static void main(String[] args) {<br \/>\n        Login login &#061; new Login();<br \/>\n        login.loginInfo(&#034;admin111&#034;, &#034;123456&#034;);<br \/>\n    }<br \/>\n} <\/p>\n<p>\u6b64\u65f6\u6211\u4eec\u5728\u5904\u7406\u7528\u6237\u540d\u5bc6\u7801\u9519\u8bef\u7684\u65f6\u5019\u53ef\u80fd\u5c31\u9700\u8981\u629b\u51fa\u4e24\u79cd\u5f02\u5e38\u3002\u6211\u4eec\u53ef\u4ee5\u57fa\u4e8e\u5df2\u6709\u7684\u5f02\u5e38\u7c7b\u8fdb\u884c\u6269\u5c55&#xff08;\u7ee7\u627f&#xff09;&#xff0c;\u521b\u5efa\u548c\u6211\u4eec\u4e1a\u52a1\u76f8\u5173\u7684\u5f02\u5e38\u7c7b\u3002<\/p>\n<p>3.1 \u5b9e\u73b0\u81ea\u5b9a\u4e49\u5f02\u5e38<\/p>\n<p>\u5177\u4f53\u65b9\u5f0f&#xff1a;<\/p>\n<li>\n<p>\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b&#xff0c;\u7136\u540e\u7ee7\u627f\u81ea Exception \u6216\u8005 RunTimeException<\/p>\n<\/li>\n<li>\n<p>\u5b9e\u73b0\u4e00\u4e2a\u5e26\u6709 String \u7c7b\u578b\u53c2\u6570\u7684\u6784\u9020\u65b9\u6cd5&#xff0c;\u53c2\u6570\u542b\u4e49&#xff1a;\u51fa\u73b0\u5f02\u5e38\u7684\u539f\u56e0<\/p>\n<\/li>\n<p>class UserNameException extends Exception {<br \/>\n    public UserNameException(String message) {<br \/>\n        super(message);<br \/>\n    }<br \/>\n}<\/p>\n<p>class PasswordException extends Exception {<br \/>\n    public PasswordException(String message) {<br \/>\n        super(message);<br \/>\n    }<br \/>\n} <\/p>\n<p>PS&#xff1a;<\/p>\n<p>Java\u5b98\u65b9\u89c4\u5b9a\u81ea\u5b9a\u4e49\u5f02\u5e38\u7ee7\u627fException\u5219\u8be5\u81ea\u5b9a\u4e49\u5f02\u5e38\u4e3a\u7f16\u8bd1\u65f6\u5f02\u5e38&#xff0c;<\/p>\n<p>\u4f46\u5f53\u5b83\u7ee7\u627fRuntimeException\u5219\u8be5\u81ea\u5b9a\u4e49\u5f02\u5e38\u4e3a\u8fd0\u884c\u65f6\u5f02\u5e38\u3002<\/p>\n<p>\u6b64\u65f6\u6211\u4eec\u7684 login \u4ee3\u7801\u53ef\u4ee5\u6539\u6210&#xff1a;<\/p>\n<p>public class Login {<br \/>\n    private String userName &#061; &#034;admin&#034;;<br \/>\n    private String password &#061; &#034;123456&#034;;<\/p>\n<p>    public void loginInfo(String userName, String password)<br \/>\n            throws UserNameException, PasswordException {<br \/>\n        if (!this.userName.equals(userName)) {<br \/>\n            throw new UserNameException(&#034;\u7528\u6237\u540d\u9519\u8bef&#xff01;&#034;);<br \/>\n        }<\/p>\n<p>        if (!this.password.equals(password)) {<br \/>\n            throw new PasswordException(&#034;\u7528\u6237\u540d\u9519\u8bef&#xff01;&#034;);<br \/>\n        }<br \/>\n        System.out.println(&#034;\u767b\u9646\u6210\u529f&#034;);<br \/>\n    }<\/p>\n<p>    public static void main(String[] args) {<br \/>\n        try {<br \/>\n            Login login &#061; new Login();<br \/>\n            login.loginInfo(&#034;admin&#034;, &#034;123456&#034;);<br \/>\n        } catch (UserNameException e) {<br \/>\n            e.printStackTrace();<br \/>\n        } catch (PasswordException e) {<br \/>\n            e.printStackTrace();<br \/>\n        }<br \/>\n    }<br \/>\n} <\/p>\n<p>\u6ce8\u610f\u4e8b\u9879<\/p>\n<ul>\n<li>\n<p>\u81ea\u5b9a\u4e49\u5f02\u5e38\u901a\u5e38\u4f1a\u7ee7\u627f\u81ea Exception \u6216\u8005 RuntimeException<\/p>\n<\/li>\n<li>\n<p>\u7ee7\u627f\u81ea Exception \u7684\u5f02\u5e38\u9ed8\u8ba4\u662f\u53d7\u67e5\u5f02\u5e38<\/p>\n<\/li>\n<li>\n<p>\u7ee7\u627f\u81ea RuntimeException \u7684\u5f02\u5e38\u9ed8\u8ba4\u662f\u975e\u53d7\u67e5\u5f02\u5e38<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h2 id=\"4.%20%E5%B0%8F%E8%AF%95%E7%89%9B%E5%88%80%EF%BC%889%E9%81%93%E9%A2%98%EF%BC%89\">4. \u5c0f\u8bd5\u725b\u5200&#xff08;9\u9053\u9898&#xff09;<\/h2>\n<p>1. \u4ee5\u4e0b\u54ea\u4e2a\u4e0d\u662fJava\u4e2d\u7684\u53d7\u68c0\u5f02\u5e38&#xff08;checked exception&#xff09;&#xff1f;<\/p>\n<p>A. IOException<\/p>\n<p>B. SQLException<\/p>\n<p>C. NullPointerException<\/p>\n<p>D. ClassNotFoundException<\/p>\n<p>\u7b54\u6848&#xff1a;C<\/p>\n<p>2. \u5173\u4e8etry-catch-finally\u8bed\u53e5&#xff0c;\u4ee5\u4e0b\u54ea\u4e2a\u8bf4\u6cd5\u662f\u6b63\u786e\u7684&#xff1f;<\/p>\n<p>A. finally\u5757\u603b\u662f\u4f1a\u6267\u884c&#xff0c;\u5373\u4f7ftry\u5757\u4e2d\u6709return\u8bed\u53e5<\/p>\n<p>B. \u5982\u679ccatch\u5757\u4e2d\u629b\u51fa\u5f02\u5e38&#xff0c;finally\u5757\u5c31\u4e0d\u4f1a\u6267\u884c<\/p>\n<p>C. \u4e00\u4e2atry\u5757\u540e\u9762\u5fc5\u987b\u8ddf\u7740\u81f3\u5c11\u4e00\u4e2acatch\u5757<\/p>\n<p>D. finally\u5757\u4e2d\u7684return\u8bed\u53e5\u4f1a\u8986\u76d6try\u6216catch\u5757\u4e2d\u7684return\u8bed\u53e5<\/p>\n<p>\u7b54\u6848&#xff1a;AD<\/p>\n<p>3. \u5173\u4e8e\u5f02\u5e38\u5904\u7406&#xff0c;\u4ee5\u4e0b\u54ea\u4e2a\u8bf4\u6cd5\u662f\u9519\u8bef\u7684&#xff1f;<\/p>\n<p>A. \u5b50\u7c7b\u65b9\u6cd5\u629b\u51fa\u7684\u5f02\u5e38\u8303\u56f4\u4e0d\u80fd\u5927\u4e8e\u7236\u7c7b\u65b9\u6cd5\u58f0\u660e\u7684\u5f02\u5e38\u8303\u56f4<\/p>\n<p>B. \u53ef\u4ee5\u5728catch\u5757\u4e2d\u6355\u83b7\u591a\u4e2a\u5f02\u5e38\u7c7b\u578b<\/p>\n<p>C. RuntimeException\u53ca\u5176\u5b50\u7c7b\u90fd\u662f\u975e\u53d7\u68c0\u5f02\u5e38<\/p>\n<p>D. \u4f7f\u7528throw\u5173\u952e\u5b57\u53ef\u4ee5\u624b\u52a8\u629b\u51fa\u5f02\u5e38&#xff0c;\u4f46\u4e0d\u80fd\u629b\u51faError<\/p>\n<p>\u7b54\u6848&#xff1a;D&#xff0c;&#xff08;\u6bd4\u5982\u4e0b\u56fe&#xff1a;&#xff09;\u867d\u7136\u8bed\u6cd5\u652f\u6301&#xff0c;\u4f46\u4e0d\u5efa\u8bae\u8fd9\u4e48\u505a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"67\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260224104324-699d80cc8d9fb.png\" width=\"253\" \/><\/p>\n<p>4. \u8bf7\u5206\u6790\u7a0b\u5e8f\u4f1a\u8f93\u51fa\u4ec0\u4e48\u5185\u5bb9<\/p>\n<p> public static void main(String[] args) {<br \/>\n   func();<br \/>\n   System.out.println(&#034;main&#xff1a;after try catch&#034;);<br \/>\n}<\/p>\n<p>public static void func() {<br \/>\n     try {<br \/>\n        int[] array &#061; {1, 2, 3};<br \/>\n        System.out.println(array[100]);<br \/>\n    } catch (ArrayIndexOutOfBoundsException e) {<br \/>\n        e.printStackTrace();<br \/>\n    }<br \/>\n    System.out.println(&#034;func&#xff1a;after try catch&#034;);<br \/>\n}<\/p>\n<p>\/\/\u8fd9\u4e2a\u4f1a\u8f93\u51fa\u4ec0\u4e48&#xff1f;\u600e\u4e48\u5206\u6790&#xff1f; <\/p>\n<p>\u9884\u671f\u8f93\u51fa\u7ed3\u679c&#xff1a;<\/p>\n<p>java.lang.ArrayIndexOutOfBoundsException: Index 100 out of bounds for length 3 \u00a0 \u00a0 at YourClassName.func(YourClassName.java:\u884c\u53f7) \u00a0 \u00a0 at YourClassName.main(YourClassName.java:\u884c\u53f7) func&#xff1a;after try catch main&#xff1a;after try catch<\/p>\n<p>5. Java\u5f02\u5e38\u5904\u7406\u673a\u5236<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"187\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260224104324-699d80cc9c450.png\" width=\"553\" \/><\/p>\n<p>C<\/p>\n<p>6. \u5206\u6790\u4ee3\u7801<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"358\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260224104324-699d80ccb16cf.png\" width=\"428\" \/><\/p>\n<p>\u4f1a\u6b63\u5e38\u8f93\u51fa&#xff1a;testMethod<\/p>\n<p>7. \u5206\u6790\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"493\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260224104324-699d80ccd17b9.png\" width=\"499\" \/><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"140\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260224104325-699d80cd002b0.png\" width=\"385\" \/><\/p>\n<p><span style=\"color:#be191c\">\u6392\u9664\u6cd5&#xff0c;\u56e0\u4e3a\u540c\u4e00\u65f6\u523b\u53ea\u80fd\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38<\/span><\/p>\n<p>8. \u5355\u9009<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"283\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260224104325-699d80cd14c97.png\" width=\"623\" \/><\/p>\n<p>\u7b54\u6848&#xff1a;BD<\/p>\n<p>B&#xff1a;\u7f16\u8bd1\u65f6\u5f02\u5e38\u53ef\u4ee5\u62ffthrows\u6765\u5904\u7406<\/p>\n<p>D&#xff1a;\u6bd4\u5982\u7a7a\u6307\u9488\u5f02\u5e38\u53ef\u4ee5\u7528throws\u5173\u952e\u5b57\u4e00\u76f4\u5c06\u5f02\u5e38\u5f80\u5916\u629b\u76f4\u5230\u8ba9JVM\u53bb\u5904\u7406<\/p>\n<p>9.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"217\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260224104325-699d80cd2cee7.png\" width=\"577\" \/><\/p>\n<p>C<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u76ee\u5f55 1. \u5f02\u5e38\u7684\u6982\u5ff5\u4e0e\u4f53\u7cfb\u7ed3\u6784 1.1 \u5f02\u5e38\u7684\u6982\u5ff5 1.1.1 \u7b97\u672f\u5f02\u5e38 1.1.2\u00a0\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38 1.1.3 \u7a7a\u6307\u9488\u5f02\u5e38 1.2 \u5f02\u5e38\u7684\u4f53\u7cfb\u7ed3\u6784 1.3 \u5f02\u5e38\u7684\u5206\u7c7b 1.3.1 \u7f16\u8bd1\u65f6\u5f02\u5e38 1.3.1.1 \u4f8b\u5b501&#xff1a; 1.3.1.2 \u4f8b\u5b502&#xff1a; 1.3.2\u00a0\u8fd0\u884c\u65f6\u5f02\u5e38 2. \u5f02\u5e38\u7684\u5904\u7406 2.1 \u9632\u5fa1\u5f0f\u7f16\u7a0b 2.1.1 LBYL: Look Before You Leap. \u5728\u64cd\u4f5c\u4e4b\u524d\u5c31\u505a\u5145\u5206\u7684\u68c0\u67e5\u3002\u5373&#xff1a;\u4e8b\u524d\u9632\u5fa1\u578b 2.1.2 EAFP<\/p>\n","protected":false},"author":2,"featured_media":77444,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[8500,8502,8501,99,5466,5918],"topic":[],"class_list":["post-77459","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-try-catch","tag-8502","tag-8501","tag-java","tag-5466","tag-5918"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>15. Java\u5f02\u5e38\u5904\u7406\u5168\u89e3\u6790\uff1a\u4ece\u5e95\u5c42\u539f\u7406\u5230\u5b9e\u6218\u907f\u5751\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/77459.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"15. Java\u5f02\u5e38\u5904\u7406\u5168\u89e3\u6790\uff1a\u4ece\u5e95\u5c42\u539f\u7406\u5230\u5b9e\u6218\u907f\u5751\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u76ee\u5f55 1. \u5f02\u5e38\u7684\u6982\u5ff5\u4e0e\u4f53\u7cfb\u7ed3\u6784 1.1 \u5f02\u5e38\u7684\u6982\u5ff5 1.1.1 \u7b97\u672f\u5f02\u5e38 1.1.2\u00a0\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38 1.1.3 \u7a7a\u6307\u9488\u5f02\u5e38 1.2 \u5f02\u5e38\u7684\u4f53\u7cfb\u7ed3\u6784 1.3 \u5f02\u5e38\u7684\u5206\u7c7b 1.3.1 \u7f16\u8bd1\u65f6\u5f02\u5e38 1.3.1.1 \u4f8b\u5b501&#xff1a; 1.3.1.2 \u4f8b\u5b502&#xff1a; 1.3.2\u00a0\u8fd0\u884c\u65f6\u5f02\u5e38 2. \u5f02\u5e38\u7684\u5904\u7406 2.1 \u9632\u5fa1\u5f0f\u7f16\u7a0b 2.1.1 LBYL: Look Before You Leap. \u5728\u64cd\u4f5c\u4e4b\u524d\u5c31\u505a\u5145\u5206\u7684\u68c0\u67e5\u3002\u5373&#xff1a;\u4e8b\u524d\u9632\u5fa1\u578b 2.1.2 EAFP\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/77459.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-24T10:43:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260224104323-699d80cb38a5c.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/77459.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/77459.html\",\"name\":\"15. Java\u5f02\u5e38\u5904\u7406\u5168\u89e3\u6790\uff1a\u4ece\u5e95\u5c42\u539f\u7406\u5230\u5b9e\u6218\u907f\u5751\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-24T10:43:26+00:00\",\"dateModified\":\"2026-02-24T10:43:26+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/77459.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/77459.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/77459.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"15. Java\u5f02\u5e38\u5904\u7406\u5168\u89e3\u6790\uff1a\u4ece\u5e95\u5c42\u539f\u7406\u5230\u5b9e\u6218\u907f\u5751\u6307\u5357\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"15. Java\u5f02\u5e38\u5904\u7406\u5168\u89e3\u6790\uff1a\u4ece\u5e95\u5c42\u539f\u7406\u5230\u5b9e\u6218\u907f\u5751\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/77459.html","og_locale":"zh_CN","og_type":"article","og_title":"15. Java\u5f02\u5e38\u5904\u7406\u5168\u89e3\u6790\uff1a\u4ece\u5e95\u5c42\u539f\u7406\u5230\u5b9e\u6218\u907f\u5751\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u76ee\u5f55 1. \u5f02\u5e38\u7684\u6982\u5ff5\u4e0e\u4f53\u7cfb\u7ed3\u6784 1.1 \u5f02\u5e38\u7684\u6982\u5ff5 1.1.1 \u7b97\u672f\u5f02\u5e38 1.1.2\u00a0\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38 1.1.3 \u7a7a\u6307\u9488\u5f02\u5e38 1.2 \u5f02\u5e38\u7684\u4f53\u7cfb\u7ed3\u6784 1.3 \u5f02\u5e38\u7684\u5206\u7c7b 1.3.1 \u7f16\u8bd1\u65f6\u5f02\u5e38 1.3.1.1 \u4f8b\u5b501&#xff1a; 1.3.1.2 \u4f8b\u5b502&#xff1a; 1.3.2\u00a0\u8fd0\u884c\u65f6\u5f02\u5e38 2. \u5f02\u5e38\u7684\u5904\u7406 2.1 \u9632\u5fa1\u5f0f\u7f16\u7a0b 2.1.1 LBYL: Look Before You Leap. \u5728\u64cd\u4f5c\u4e4b\u524d\u5c31\u505a\u5145\u5206\u7684\u68c0\u67e5\u3002\u5373&#xff1a;\u4e8b\u524d\u9632\u5fa1\u578b 2.1.2 EAFP","og_url":"https:\/\/www.wsisp.com\/helps\/77459.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-24T10:43:26+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260224104323-699d80cb38a5c.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"9 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/77459.html","url":"https:\/\/www.wsisp.com\/helps\/77459.html","name":"15. Java\u5f02\u5e38\u5904\u7406\u5168\u89e3\u6790\uff1a\u4ece\u5e95\u5c42\u539f\u7406\u5230\u5b9e\u6218\u907f\u5751\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-24T10:43:26+00:00","dateModified":"2026-02-24T10:43:26+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/77459.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/77459.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/77459.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"15. Java\u5f02\u5e38\u5904\u7406\u5168\u89e3\u6790\uff1a\u4ece\u5e95\u5c42\u539f\u7406\u5230\u5b9e\u6218\u907f\u5751\u6307\u5357"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/77459","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=77459"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/77459\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/77444"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=77459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=77459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=77459"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=77459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}