{"id":202907,"date":"2025-05-22T10:01:25","date_gmt":"2025-05-22T02:01:25","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202907\/"},"modified":"2025-05-22T10:01:25","modified_gmt":"2025-05-22T02:01:25","slug":"java%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b%e4%b8%ad%e7%9a%84%e5%bc%82%e5%b8%b8%e5%92%8c%e8%bf%94%e5%9b%9e%e5%80%bc%e6%9c%89%e4%bb%80%e4%b9%88%e5%85%b3%e7%b3%bb%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202907\/","title":{"rendered":"Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u7684\u5f02\u5e38\u548c\u8fd4\u56de\u503c\u6709\u4ec0\u4e48\u5173\u7cfb\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u7684\u5f02\u5e38\u548c\u8fd4\u56de\u503c\u6709\u4ec0\u4e48\u5173\u7cfb\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300aJava\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u7684\u5f02\u5e38\u548c\u8fd4\u56de\u503c\u6709\u4ec0\u4e48\u5173\u7cfb\uff1f\u300b\uff0c\u8986\u76d6\u4e86\u6587\u7ae0\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1729994380671d9e8c86bb6.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u7684\u5f02\u5e38\u548c\u8fd4\u56de\u503c<\/strong><\/p>\n<p>\u5728Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\uff0c\u5f02\u5e38\u548c\u8fd4\u56de\u503c\u6709\u7740\u7d27\u5bc6\u7684\u5173\u7cfb\u3002\u51fd\u6570\u5f0f\u7f16\u7a0b\u5021\u5bfc\u4f7f\u7528\u7eaf\u51fd\u6570\uff0c\u5373\u4e0d\u4ea7\u751f\u526f\u4f5c\u7528\u3001\u4e0d\u4fee\u6539\u8f93\u5165\u72b6\u6001\u7684\u51fd\u6570\u3002\u7136\u800c\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u96be\u514d\u4f1a\u9047\u5230\u9700\u8981\u5904\u7406\u5f02\u5e38\u7684\u60c5\u51b5\u3002<\/p>\n<p><strong>\u8fd4\u56de\u503c\u7c7b\u578b<\/strong><\/p>\n<p>\u5728Java\u4e2d\uff0c\u51fd\u6570\u5f0f\u63a5\u53e3\u53ef\u4ee5\u8fd4\u56de\u4e24\u79cd\u7c7b\u578b\u7684\u503c\uff1a<\/p>\n<ul>\n<li><strong>\u6b63\u5e38\u503c\uff1a<\/strong> \u51fd\u6570\u6b63\u5e38\u6267\u884c\u540e\u8fd4\u56de\u9884\u671f\u7684\u7ed3\u679c\u503c\u3002<\/li>\n<li><strong>Void\uff1a<\/strong> \u51fd\u6570\u6267\u884c\u4e0d\u4f1a\u8fd4\u56de\u4efb\u4f55\u503c\u3002<\/li>\n<\/ul>\n<p><strong>\u5f02\u5e38\u5904\u7406<\/strong><\/p>\n<p>\u5728\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u901a\u5e38\u901a\u8fc7\u4e24\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff1a<\/p>\n<ul>\n<li><strong>Checked Exceptions\uff1a<\/strong> \u7f16\u8bd1\u5668\u5f3a\u5236\u68c0\u67e5\u7684\u5f02\u5e38\uff0c\u5fc5\u987b\u5904\u7406\u6216\u58f0\u660e\u629b\u51fa\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u51fd\u6570\u7b7e\u540d\u4e2d\u58f0\u660e<code>throws<\/code>\u5b50\u53e5\u6765\u5904\u7406checked exceptions\u3002<\/li>\n<li><strong>Unchecked Exceptions\uff1a<\/strong> \u7f16\u8bd1\u5668\u4e0d\u5f3a\u5236\u68c0\u67e5\u7684\u5f02\u5e38\uff0c\u53ef\u4ee5\u4e0d\u5904\u7406\u6216\u58f0\u660e\u629b\u51fa\u3002 unchecked exceptions\u901a\u5e38\u7ee7\u627f\u81ea<code>RuntimeException<\/code>\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b Java \u51fd\u6570\u5f0f\u63a5\u53e3\uff1a<\/p>\n<pre>@FunctionalInterface\ninterface Operation&lt;T&gt; {\n    T apply(T input) throws Exception;\n}<\/pre>\n<p>\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u5b9e\u73b0\u8be5\u63a5\u53e3\uff1a<\/p>\n<pre>Operation&lt;Integer&gt;multiply = (x) -&gt; x * 2;<\/pre>\n<p><strong>\u5904\u7406Checked Exception<\/strong><\/p>\n<p>\u5982\u679c\u51fd\u6570\u53ef\u80fd\u4f1a\u629b\u51fa checked exception\uff0c\u5fc5\u987b\u5728\u51fd\u6570\u7b7e\u540d\u4e2d\u58f0\u660e<code>throws<\/code>\u5b50\u53e5\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>Operation&lt;String&gt;parseInt = (s) -&gt; {\n    try {\n        return Integer.parseInt(s);\n    } catch (NumberFormatException e) {\n        throw new RuntimeException(e);\n    }\n};<\/pre>\n<p>\u8c03\u7528\u65f6\uff0c\u5fc5\u987b\u4f7f\u7528<code>try-catch<\/code>\u5757\u5904\u7406 checked exception\uff1a<\/p>\n<pre>try {\n    int result = parseInt.apply(\"123\");\n} catch (RuntimeException e) {\n    \/\/ \u5904\u7406 NumberFormatException\n}<\/pre>\n<p><strong>\u5ffd\u7565Unchecked Exception<\/strong><\/p>\n<p>\u5bf9\u4e8e unchecked exception\uff0c\u53ef\u4ee5\u4e0d\u5904\u7406\u6216\u58f0\u660e\u629b\u51fa\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>Operation&lt;Integer&gt;divide = (x) -&gt; {\n    return x \/ 0; \/\/ \u53ef\u80fd\u629b\u51fa ArithmeticException\n};<\/pre>\n<p>\u8c03\u7528\u65f6\uff0c\u53ef\u4ee5\u9009\u62e9\u5ffd\u7565 unchecked exception\uff1a<\/p>\n<pre>try {\n    int result = divide.apply(10);\n} catch (Exception e) {\n    \/\/ \u4e0d\u505a\u5904\u7406\n}<\/pre>\n<p><strong>\u6700\u4f73\u5b9e\u8df5<\/strong><\/p>\n<p>\u5728\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\uff0c\u6700\u4f73\u5b9e\u8df5\u662f\u5c3d\u91cf\u4f7f\u7528 pure \u51fd\u6570\u3002\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u5f02\u5e38\u7684\u60c5\u51b5\uff0c\u5efa\u8bae\u4f7f\u7528 checked exception\u5e76\u660e\u786e\u901a\u8fc7<code>try-catch<\/code>\u5757\u5904\u7406\u5f02\u5e38\u3002\u5c3d\u91cf\u907f\u514d\u4f7f\u7528 unchecked exception\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u5bfc\u81f4\u610f\u5916\u7684\u7a0b\u5e8f\u884c\u4e3a\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aJava\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u7684\u5f02\u5e38\u548c\u8fd4\u56de\u503c\u6709\u4ec0\u4e48\u5173\u7cfb\uff1f\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u5f02\u5e38,\u8fd4\u56de\u503c\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u7684\u5f02\u5e38\u548c\u8fd4\u56de&#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-202907","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202907","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=202907"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202907\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202907"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202907"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202907"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}