{"id":202784,"date":"2025-05-22T09:08:01","date_gmt":"2025-05-22T01:08:01","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202784\/"},"modified":"2025-05-22T09:08:01","modified_gmt":"2025-05-22T01:08:01","slug":"%e5%9c%a8-java-%e4%b8%ad%e5%a4%84%e7%90%86%e4%b8%8d%e5%90%8c%e7%b1%bb%e5%9e%8b%e7%9a%84%e5%bc%82%e5%b8%b8%e7%9a%84%e6%9c%80%e4%bd%b3%e6%96%b9%e5%bc%8f%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202784\/","title":{"rendered":"\u5728 Java \u4e2d\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38\u7684\u6700\u4f73\u65b9\u5f0f\u662f\u4ec0\u4e48\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5728 Java \u4e2d\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38\u7684\u6700\u4f73\u65b9\u5f0f\u662f\u4ec0\u4e48\uff1f<\/h1>\n<p>\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5728 Java \u4e2d\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38\u7684\u6700\u4f73\u65b9\u5f0f\u662f\u4ec0\u4e48\uff1f\u300b<\/span>\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01<\/p>\n<p>\u5728Java\u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u5065\u58ee\u6027\uff1a\u5df2\u68c0\u5f02\u5e38\u5fc5\u987b\u901a\u8fc7try-catch\u5757\u6216throws\u58f0\u660e\u663e\u5f0f\u5904\u7406\u3002\u672a\u68c0\u5f02\u5e38\u4e0d\u9700\u8981\u663e\u5f0f\u5904\u7406\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528try-catch\u5757\u6355\u83b7\u548c\u5904\u7406\u3002\u5b9e\u6218\u6848\u4f8b\u5c55\u793a\u4e86\u4f7f\u7528try-catch\u5757\u548cthrows\u58f0\u660e\u5904\u7406\u5f02\u5e38\u7684\u60c5\u51b5\u3002\u6700\u4f73\u5b9e\u8df5\u5305\u62ec\u59cb\u7ec8\u6355\u83b7\u6240\u6709\u5df2\u68c0\u5f02\u5e38\uff0c\u4ec5\u5728\u65e0\u6cd5\u6355\u83b7\u6216\u5904\u7406\u672a\u68c0\u5f02\u5e38\u65f6\u624d\u629b\u51fa\uff0c\u4f7f\u7528e.printStackTrace()\u6253\u5370\u5f02\u5e38\u5806\u6808\u8ddf\u8e2a\uff0c\u5e76\u4f7f\u7528\u5f02\u5e38\u65e5\u5fd7\u8bb0\u5f55\u6846\u67b6\u8bb0\u5f55\u91cd\u8981\u5f02\u5e38\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729941686671cd0b60dd55.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5728 Java \u4e2d\u4f18\u96c5\u5730\u5904\u7406\u5f02\u5e38<\/strong><\/p>\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n<p>\u5f02\u5e38\u662f Java \u7f16\u7a0b\u4e2d\u4e0d\u53ef\u907f\u514d\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u4eec\u5141\u8bb8\u5e94\u7528\u7a0b\u5e8f\u5bf9\u9884\u671f\u7684\u9519\u8bef\u60c5\u51b5\u505a\u51fa\u53cd\u5e94\u3002\u5904\u7406\u5f02\u5e38\u65f6\uff0c\u9075\u5b88\u6700\u4f73\u5b9e\u8df5\u81f3\u5173\u91cd\u8981\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u9c81\u68d2\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<p><strong>\u5f02\u5e38\u79cd\u7c7b<\/strong><\/p>\n<p>Java \u4e2d\u7684\u5f02\u5e38\u5206\u4e3a\u4e24\u5927\u7c7b\uff1a\u5df2\u68c0\u5f02\u5e38\uff08Checked Exception\uff09\u548c\u672a\u68c0\u5f02\u5e38\uff08Unchecked Exception\uff09\u3002<\/p>\n<ul>\n<li><strong>\u5df2\u68c0\u5f02\u5e38\uff1a<\/strong> \u9700\u8981\u5728\u7f16\u8bd1\u65f6\u901a\u8fc7 <code>throws<\/code> \u5173\u952e\u5b57\u663e\u5f0f\u58f0\u660e\u7684\u5f02\u5e38\uff0c\u4f8b\u5982 IOException\u3001SQLException\u3002<\/li>\n<li><strong>\u672a\u68c0\u5f02\u5e38\uff1a<\/strong> JVM \u5728\u8fd0\u884c\u65f6\u629b\u51fa\u7684\u5f02\u5e38\uff0c\u4e0d\u9700\u8981\u663e\u5f0f\u58f0\u660e\uff0c\u4f8b\u5982 NullPointerException\u3001ArrayIndexOutOfBoundsException\u3002<\/li>\n<\/ul>\n<p><strong>\u5904\u7406\u5df2\u68c0\u5f02\u5e38<\/strong><\/p>\n<p>\u4e3a\u4e86\u5904\u7406\u5df2\u68c0\u5f02\u5e38\uff0c\u5fc5\u987b\u4f7f\u7528 <code>try-catch<\/code> \u5757\u6216 <code>throws<\/code> \u58f0\u660e\u3002<\/p>\n<pre>try {\n    \/\/ \u53ef\u80fd\u629b\u51fa\u5df2\u68c0\u5f02\u5e38\u7684\u4ee3\u7801\n} catch (IOException e) {\n    \/\/ \u5904\u7406 IOException \u5f02\u5e38\n}<\/pre>\n<p>\u4e5f\u53ef\u4ee5\u4f7f\u7528 <code>throws<\/code> \u58f0\u660e\u5c06\u5df2\u68c0\u5f02\u5e38\u4f20\u64ad\u5230\u8c03\u7528\u65b9\u6cd5\u3002<\/p>\n<pre>public void readFile() throws IOException {\n    \/\/ \u53ef\u80fd\u629b\u51fa IOException \u7684\u4ee3\u7801\n}<\/pre>\n<p><strong>\u5904\u7406\u672a\u68c0\u5f02\u5e38<\/strong><\/p>\n<p>\u672a\u68c0\u5f02\u5e38\u4e0d\u9700\u8981\u663e\u5f0f\u5904\u7406\uff0c\u4f46\u4ecd\u53ef\u4ee5\u4f7f\u7528 <code>try-catch<\/code> \u5757\u6765\u6355\u83b7\u548c\u5904\u7406\u5b83\u4eec\u3002<\/p>\n<pre>try {\n    \/\/ \u53ef\u80fd\u629b\u51fa\u672a\u68c0\u5f02\u5e38\u7684\u4ee3\u7801\n} catch (NullPointerException e) {\n    \/\/ \u5904\u7406 NullPointerException \u5f02\u5e38\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p><strong>\u793a\u4f8b 1\uff1a\u4f7f\u7528 <code>try-catch<\/code> \u5757<\/strong><\/p>\n<p>\u8003\u8651\u4e00\u4e2a\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u7684\u51fd\u6570\u3002\u5b83\u53ef\u80fd\u629b\u51fa\u4e00\u4e2a <code>IOException<\/code>\u3002<\/p>\n<pre>public String readFile(String path) {\n    try {\n        return new String(Files.readAllBytes(Paths.get(path)));\n    } catch (IOException e) {\n        e.printStackTrace();\n        return \"\";\n    }\n}<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a\u4f7f\u7528 <code>throws<\/code> \u58f0\u660e<\/strong><\/p>\n<p>\u8003\u8651\u4e00\u4e2a\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u53ef\u80fd\u629b\u51fa\u4e00\u4e2a <code>SQLException<\/code>\u3002<\/p>\n<pre>public void updateDatabase(String query) throws SQLException {\n    Connection connection = ...;\n    Statement statement = connection.createStatement();\n    statement.executeUpdate(query);\n}<\/pre>\n<p><strong>\u6700\u4f73\u5b9e\u8df5<\/strong><\/p>\n<ul>\n<li>\u59cb\u7ec8\u5c1d\u8bd5\u6355\u83b7\u5e76\u5904\u7406\u6240\u6709\u5df2\u68c0\u5f02\u5e38\u3002<\/li>\n<li>\u4ec5\u5728\u65e0\u6cd5\u6355\u83b7\u6216\u5904\u7406\u672a\u68c0\u5f02\u5e38\u65f6\u624d\u629b\u51fa\u5b83\u4eec\u3002<\/li>\n<li>\u4f7f\u7528 <code>e.printStackTrace()<\/code> \u6253\u5370\u51fa\u5f02\u5e38\u5806\u6808\u8ddf\u8e2a\u4ee5\u8fdb\u884c\u8c03\u8bd5\u3002<\/li>\n<li>\u8003\u8651\u4f7f\u7528\u5f02\u5e38\u65e5\u5fd7\u8bb0\u5f55\u6846\u67b6\uff08\u5982 Log4j\uff09\u8bb0\u5f55\u91cd\u8981\u5f02\u5e38\u3002<\/li>\n<\/ul>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u5728 Java \u4e2d\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38\u7684\u6700\u4f73\u65b9\u5f0f\u662f\u4ec0\u4e48\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Java \u4e2d\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u5f02&#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-202784","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202784","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=202784"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202784\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202784"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}