{"id":202948,"date":"2025-05-22T09:43:12","date_gmt":"2025-05-22T01:43:12","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202948\/"},"modified":"2025-05-22T09:43:12","modified_gmt":"2025-05-22T01:43:12","slug":"java-%e5%87%bd%e6%95%b0%e4%b8%ad%e7%9a%84%e5%bc%82%e5%b8%b8%e7%b1%bb%e5%9e%8b%e6%9c%89%e5%93%aa%e4%ba%9b%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202948\/","title":{"rendered":"Java \u51fd\u6570\u4e2d\u7684\u5f02\u5e38\u7c7b\u578b\u6709\u54ea\u4e9b\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Java \u51fd\u6570\u4e2d\u7684\u5f02\u5e38\u7c7b\u578b\u6709\u54ea\u4e9b\uff1f<\/h1>\n<p>\u6765\u5230\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300aJava \u51fd\u6570\u4e2d\u7684\u5f02\u5e38\u7c7b\u578b\u6709\u54ea\u4e9b\uff1f\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p>Java \u51fd\u6570\u4e2d\u7684\u5f02\u5e38\u7c7b\u578b\u5206\u4e3a\u4e24\u79cd\uff1a\u5df2\u68c0\u67e5\u5f02\u5e38\uff08\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u58f0\u660e\uff09\u548c\u672a\u68c0\u67e5\u5f02\u5e38\uff08\u65e0\u9700\u58f0\u660e\uff09\u3002\u4f7f\u7528 throws \u5173\u952e\u5b57\u53ef\u4ee5\u5728\u51fd\u6570\u7b7e\u540d\u4e2d\u58f0\u660e\u51fd\u6570\u53ef\u80fd\u629b\u51fa\u7684\u5f02\u5e38\uff0c\u8c03\u7528\u8005\u53ef\u4ee5\u5904\u7406\u6216\u91cd\u65b0\u58f0\u660e\u5f02\u5e38\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730003563671dc26ba1f52.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>Java \u51fd\u6570\u4e2d\u7684\u5f02\u5e38\u7c7b\u578b<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u5f02\u5e38\u662f\u7528\u6765\u5904\u7406\u9519\u8bef\u548c\u5176\u4ed6\u5f02\u5e38\u6761\u4ef6\u7684\u4e8b\u4ef6\u3002\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 <code>throws<\/code> \u5173\u952e\u5b57\u6765\u58f0\u660e\u6f5c\u5728\u7684\u5f02\u5e38\u7c7b\u578b\uff0c\u4ee5\u4fbf\u8c03\u7528\u8005\u53ef\u4ee5\u76f8\u5e94\u5730\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002<\/p>\n<p><strong>\u5f02\u5e38\u7c7b\u578b<\/strong><\/p>\n<p>Java \u4e2d\u7684\u5f02\u5e38\u7c7b\u578b\u53ef\u4ee5\u5206\u4e3a\u4e24\u5927\u7c7b\uff1a<\/p>\n<ul>\n<li><strong>\u5df2\u68c0\u67e5\u5f02\u5e38\uff08Checked Exceptions\uff09<\/strong>\uff1a\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u58f0\u660e\u7684\u5f02\u5e38\uff0c\u4f8b\u5982 <code>IOException<\/code> \u548c <code>SQLException<\/code>\u3002\u8fd9\u4e9b\u5f02\u5e38\u8868\u793a\u4e25\u91cd\u9519\u8bef\uff0c\u5982\u679c\u4e0d\u5904\u7406\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002<\/li>\n<li><strong>\u672a\u68c0\u67e5\u5f02\u5e38\uff08Unchecked Exceptions\uff09<\/strong>\uff1a\u4e0d\u9700\u8981\u5728\u7f16\u8bd1\u65f6\u58f0\u660e\u7684\u5f02\u5e38\uff0c\u4f8b\u5982 <code>NullPointerException<\/code> \u548c <code>ArrayIndexOutOfBoundsException<\/code>\u3002\u8fd9\u4e9b\u5f02\u5e38\u901a\u5e38\u8868\u793a\u7f16\u7a0b\u9519\u8bef\uff0c\u5728\u8fd0\u884c\u65f6\u53ef\u4ee5\u5904\u7406\u3002<\/li>\n<\/ul>\n<p><strong>\u4f7f\u7528 <code>throws<\/code> \u58f0\u660e<\/strong><\/p>\n<p>\u8981\u58f0\u660e\u51fd\u6570\u53ef\u80fd\u629b\u51fa\u7684\u5f02\u5e38\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>throws<\/code> \u5173\u952e\u5b57\uff0c\u540e\u8ddf\u5f02\u5e38\u7c7b\u578b\u7684\u5168\u9650\u5b9a\u540d\u79f0\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>public void openFile(String path) throws IOException {\n  \/\/ \u4ee3\u7801...\n}<\/pre>\n<p>\u6b64\u51fd\u6570\u58f0\u660e\u53ef\u80fd\u629b\u51fa <code>IOException<\/code> \u5f02\u5e38\u3002\u5982\u679c\u786e\u5b9e\u53d1\u751f\u4e86\u5f02\u5e38\uff0c\u8c03\u7528\u8005\u5fc5\u987b\u4f7f\u7528 <code>try-catch<\/code> \u5757\u6765\u5904\u7406\u5f02\u5e38\uff0c\u6216\u8005\u5728\u51fd\u6570\u7b7e\u540d\u4e2d\u91cd\u65b0\u58f0\u660e\u5f02\u5e38\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4e00\u4e2a\u6253\u5f00\u6587\u4ef6\u5e76\u8bfb\u53d6\u5176\u5185\u5bb9\u7684\u51fd\u6570\uff1a<\/p>\n<pre>public static String readFile(String path) throws IOException {\n  try (BufferedReader reader = new BufferedReader(new FileReader(path))) {\n    StringBuilder content = new StringBuilder();\n    String line = null;\n    while ((line = reader.readLine()) != null) {\n      content.append(line).append(\"\\n\");\n    }\n    return content.toString();\n  }\n}<\/pre>\n<p>\u6b64\u51fd\u6570\u4f7f\u7528 <code>try-with-resources<\/code> \u8bed\u53e5\u6253\u5f00\u6587\u4ef6\u5e76\u8bfb\u53d6\u5185\u5bb9\u3002\u5982\u679c\u53d1\u751f <code>IOException<\/code>\uff0c\u6b64\u5f02\u5e38\u5c06\u88ab\u6355\u83b7\u5e76\u629b\u7ed9\u8c03\u7528\u8005\u3002\u8c03\u7528\u8005\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\u5904\u7406\u5f02\u5e38\uff1a<\/p>\n<pre>try {\n  String content = readFile(\"my_file.txt\");\n} catch (IOException e) {\n  \/\/ \u5904\u7406\u5f02\u5e38...\n}<\/pre>\n<p>\u6587\u4e2d\u5173\u4e8ejava,\u5f02\u5e38\u7c7b\u578b\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300aJava \u51fd\u6570\u4e2d\u7684\u5f02\u5e38\u7c7b\u578b\u6709\u54ea\u4e9b\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java \u51fd\u6570\u4e2d\u7684\u5f02\u5e38\u7c7b\u578b\u6709\u54ea\u4e9b&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-202948","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202948","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202948"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202948\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}