{"id":203104,"date":"2025-05-22T11:46:17","date_gmt":"2025-05-22T03:46:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203104\/"},"modified":"2025-05-22T11:46:17","modified_gmt":"2025-05-22T03:46:17","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8java%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b%e4%b8%ad%e6%8a%9b%e5%87%ba%e5%bc%82%e5%b8%b8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203104\/","title":{"rendered":"\u5982\u4f55\u5728Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u629b\u51fa\u5f02\u5e38\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u5728Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u629b\u51fa\u5f02\u5e38\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\u300a\u5982\u4f55\u5728Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u629b\u51fa\u5f02\u5e38\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>\u5728 Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u629b\u51fa\u5f02\u5e38\u7684\u65b9\u5f0f\u6709\uff1a\u4f7f\u7528 try-catch \u5757\uff0c\u53ef\u5728\u4ee3\u7801\u5757\u4e2d\u6355\u83b7\u5f02\u5e38\u5e76\u5904\u7406\u3002\u4f7f\u7528 Either \u7c7b\uff0c\u53ef\u5c06\u7ed3\u679c\u5c01\u88c5\u4e3a Right\uff08\u6210\u529f\uff09\u6216 Left\uff08\u9519\u8bef\uff09\uff0c\u4ee5\u5904\u7406\u6f5c\u5728\u5f02\u5e38\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241028\/1730091467671f19cb921bb.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u5728 Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u629b\u51fa\u5f02\u5e38<\/strong><\/p>\n<p>Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u63d0\u4f9b\u4e86\u7b80\u6d01\u7684\u65b9\u6cd5\u6765\u64cd\u4f5c\u6570\u636e\uff0c\u4f46\u5728\u9700\u8981\u629b\u51fa\u5f02\u5e38\u65f6\u5374\u9047\u5230\u4e86\u6311\u6218\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5728 Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u629b\u51fa\u5f02\u5e38\u7684\u6709\u6548\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u5b9e\u6218\u6848\u4f8b\u4f9b\u53c2\u8003\u3002<\/p>\n<p><strong>1. \u4f7f\u7528 <code>Try-Catch<\/code> \u5757<\/strong><\/p>\n<p><code>try-catch<\/code> \u5757\u662f Java \u4e2d\u5904\u7406\u5f02\u5e38\u7684\u4f20\u7edf\u65b9\u5f0f\u3002\u5b83\u5141\u8bb8\u60a8\u6355\u83b7\u53ef\u80fd\u629b\u51fa\u7684\u4efb\u4f55\u5f02\u5e38\u5e76\u5728\u5757\u4e2d\u8fdb\u884c\u76f8\u5e94\u5904\u7406\u3002\u5728\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>try-catch<\/code> \u5757\u6765\u629b\u51fa\u5f02\u5e38\uff1a<\/p>\n<pre>Function&lt;String, String&gt; f = (s) -&gt; {\n  try {\n    return s.toUpperCase();\n  } catch (Exception e) {\n    throw new RuntimeException(e);\n  }\n};<\/pre>\n<p><strong>2. \u4f7f\u7528 <code>Either<\/code> \u7c7b<\/strong><\/p>\n<p>Java \u4e2d\u7684 <code>Either<\/code> \u7c7b\u63d0\u4f9b\u4e86\u5904\u7406\u6210\u529f\u6216\u5931\u8d25\u7ed3\u679c\u7684\u901a\u7528\u65b9\u6cd5\u3002\u5b83\u53ef\u4ee5\u8868\u793a\u4e24\u79cd\u53ef\u80fd\u7684\u503c\u4e4b\u4e00\uff1a\u8981\u4e48\u662f <code>Right<\/code>\uff08\u5305\u542b\u8ba1\u7b97\u7ed3\u679c\uff09\u6216 <code>Left<\/code>\uff08\u5305\u542b\u9519\u8bef\u6d88\u606f\uff09\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>Either<\/code> \u7c7b\u629b\u51fa\u5f02\u5e38\u5982\u4e0b\uff1a<\/p>\n<pre>Function&lt;String, Either&lt;String, String&gt;&gt; g = (s) -&gt; {\n  try {\n    return Right.of(s.toUpperCase());\n  } catch (Exception e) {\n    return Left.of(e.getMessage());\n  }\n};<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5927\u5199\u7684\u51fd\u6570\u5f0f\u6848\u4f8b\uff0c\u5b83\u4f7f\u7528 <code>Either<\/code> \u7c7b\u6765\u5904\u7406\u6f5c\u5728\u7684\u5f02\u5e38\uff1a<\/p>\n<pre>String input = \"hello\";\n\nFunction&lt;String, Either&lt;String, String&gt;&gt; g = (s) -&gt; {\n  try {\n    return Right.of(s.toUpperCase());\n  } catch (Exception e) {\n    return Left.of(e.getMessage());\n  }\n};\n\nEither&lt;String, String&gt; result = g.apply(input);\n\nif (result.isRight()) {\n  System.out.println(result.get());\n} else {\n  System.out.println(\"Error: \" + result.getError());\n}<\/pre>\n<p>\u6267\u884c\u6b64\u4ee3\u7801\u5c06\u8f93\u51fa\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<pre>HELLO<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u6848\u4f8b\u4e2d\uff0c<code>g<\/code> \u51fd\u6570\u4f7f\u7528 <code>try-catch<\/code> \u5757\u6765\u6355\u6349\u6f5c\u5728\u7684\u5f02\u5e38\uff0c\u5e76\u4f7f\u7528 <code>Either<\/code> \u7c7b\u5c06\u7ed3\u679c\u5305\u88c5\u4e3a <code>Right<\/code>\uff08\u6210\u529f\uff09\u6216 <code>Left<\/code>\uff08\u9519\u8bef\uff09\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u7ed3\u679c\u7684\u503c\u6267\u884c\u76f8\u5e94\u7684\u4ee3\u7801\u903b\u8f91\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u5728Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u629b\u51fa\u5f02\u5e38\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u629b\u51fa\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-203104","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203104","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=203104"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203104\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}