{"id":202615,"date":"2025-05-22T10:38:07","date_gmt":"2025-05-22T02:38:07","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202615\/"},"modified":"2025-05-22T10:38:07","modified_gmt":"2025-05-22T02:38:07","slug":"java-%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b%e4%b8%ad%e7%9a%84%e9%94%99%e8%af%af%e5%a4%84%e7%90%86%e7%ad%96%e7%95%a5%e6%9c%89%e5%93%aa%e4%ba%9b%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202615\/","title":{"rendered":"Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u7684\u9519\u8bef\u5904\u7406\u7b56\u7565\u6709\u54ea\u4e9b\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u7684\u9519\u8bef\u5904\u7406\u7b56\u7565\u6709\u54ea\u4e9b\uff1f<\/h1>\n<p>\u73cd\u60dc\u65f6\u95f4\uff0c\u52e4\u594b\u5b66\u4e60\uff01\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aJava \u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u7684\u9519\u8bef\u5904\u7406\u7b56\u7565\u6709\u54ea\u4e9b\uff1f\u300b<\/span>\uff0c\u6b63\u6587\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u6216\u8005\u662f\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u6709\u7591\u95ee\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u6211\uff01\u540e\u9762\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u5185\u5bb9\u7684\uff0c\u5e0c\u671b\u90fd\u80fd\u5e2e\u5230\u6b63\u5728\u5b66\u4e60\u7684\u5927\u5bb6\uff01<\/p>\n<p>\u51fd\u6570\u5f0f Java \u4e2d\u7684\u9519\u8bef\u5904\u7406\u5305\u62ec\u51e0\u79cd\u65b9\u6cd5\uff1aTry-Catch \u5757\u3001Optional\uff08\u8868\u793a\u503c\u5b58\u5728\u6027\uff09\u3001Result\uff08\u8868\u793a\u8ba1\u7b97\u7ed3\u679c\u72b6\u6001\uff09\u3001Either\uff08\u81ea\u5b9a\u4e49\u6210\u529f\u548c\u5931\u8d25\u7c7b\u578b\uff09\u3002Optional \u5b9e\u6218\u793a\u4f8b\uff1a\u4f7f\u7528 Optional.ofNullable() \u5904\u7406\u53ef\u80fd\u4e3a\u7a7a\u7684\u503c\uff0c\u907f\u514d NullPointerException \u5e76\u63d0\u9ad8\u4ee3\u7801\u53ef\u7ef4\u62a4\u6027\u3002\u5176\u4ed6\u7b56\u7565\uff08Result \u548c Either\uff09\u5728\u590d\u6742\u573a\u666f\u4e2d\u63d0\u4f9b\u66f4\u591a\u7075\u6d3b\u6027\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729924486671c8d8654fca.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u7684\u9519\u8bef\u5904\u7406\u7b56\u7565<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u7f16\u7a0b\u5f3a\u8c03\u4e0d\u53ef\u53d8\u6027\u3001\u7eaf\u51fd\u6570\u548c\u4e0d\u53d8\u6027\u3002\u5b83\u63d0\u4f9b\u4e86\u51e0\u79cd\u673a\u5236\u6765\u5904\u7406\u9519\u8bef\uff0c\u5305\u62ec\uff1a<\/p>\n<p><strong>1. Try-Catch \u5757<\/strong><\/p>\n<p>\u4f20\u7edf\u7684\u65b9\u6cd5\u4ecd\u7136\u9002\u7528\u4e8e\u51fd\u6570\u5f0f\u7f16\u7a0b\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>try-catch<\/code> \u5757\u6355\u83b7\u9519\u8bef\u3002\u4f46\u662f\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u4ee3\u7801\u5197\u957f\u4e14\u96be\u4ee5\u7ef4\u62a4\u3002<\/p>\n<pre>try {\n    \/\/ \u6267\u884c\u53ef\u80fd\u4f1a\u5f15\u53d1\u9519\u8bef\u7684\u4ee3\u7801\n} catch (Exception e) {\n    \/\/ \u9519\u8bef\u5904\u7406\u903b\u8f91\n}<\/pre>\n<p><strong>2. Optional \u548c Result<\/strong><\/p>\n<p><code>Optional<\/code> \u7c7b\u7528\u4e8e\u8868\u793a\u53ef\u80fd\u5b58\u5728\u6216\u4e0d\u5b58\u5728\u7684\u503c\u3002\u5b83\u6709\u4e24\u79cd\u72b6\u6001\uff1a<code>isPresent()<\/code> \u4e3a <code>true<\/code>\uff0c\u8868\u793a\u6709\u503c\uff1b<code>isPresent()<\/code> \u4e3a <code>false<\/code>\uff0c\u8868\u793a\u6ca1\u6709\u503c\u3002<\/p>\n<pre>Optional&lt;String&gt; optionalValue = Optional.of(\"Hello\");\nif (optionalValue.isPresent()) {\n    String value = optionalValue.get();\n}<\/pre>\n<p><code>Result<\/code> \u7c7b\u8868\u793a\u4e00\u4e2a\u8ba1\u7b97\u7684\u7ed3\u679c\uff0c\u5b83\u53ef\u4ee5\u662f <code>Ok<\/code> \u6216 <code>Err<\/code>\u3002<code>Ok<\/code> \u8868\u793a\u8ba1\u7b97\u6210\u529f\u5e76\u5305\u542b\u7ed3\u679c\u503c\uff0c\u800c <code>Err<\/code> \u8868\u793a\u8ba1\u7b97\u5931\u8d25\u5e76\u5305\u542b\u9519\u8bef\u6d88\u606f\u3002<\/p>\n<pre>Result&lt;Integer, String&gt; result = Result.ok(10);\nif (result.isOk()) {\n    int value = result.get();\n} else {\n    String errorMessage = result.getError();\n}<\/pre>\n<p><strong>3. Either<\/strong><\/p>\n<p><code>Either<\/code> \u7c7b\u662f\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u5904\u7406\u9519\u8bef\u7684\u53e6\u4e00\u79cd\u6d41\u884c\u65b9\u6cd5\u3002\u5b83\u7c7b\u4f3c\u4e8e <code>Result<\/code>\uff0c\u4f46\u5141\u8bb8\u81ea\u5b9a\u4e49\u6210\u529f\u548c\u5931\u8d25\u7c7b\u578b\u3002<\/p>\n<pre>Either&lt;String, Integer&gt; eitherValue = Either.right(10); \/\/ \u6210\u529f\nif (eitherValue.isRight()) {\n    int value = eitherValue.right().get();\n} else {\n    String errorMessage = eitherValue.left().get();\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u4f7f\u7528 <code>Optional<\/code> \u5904\u7406\u53ef\u80fd\u4e3a\u7a7a\u7684\u503c\uff1a<\/p>\n<pre>public class CustomerService {\n\n    public static Optional&lt;Customer&gt; getCustomerById(int id) {\n        \/\/ \u4ece\u6570\u636e\u5e93\u83b7\u53d6\u5ba2\u6237\uff0c\u5982\u679c\u627e\u4e0d\u5230\uff0c\u5219\u8fd4\u56de\u7a7a\n        Customer customer = ...;\n        return Optional.ofNullable(customer);\n    }\n}\n\npublic class Main {\n\n    public static void main(String[] args) {\n        Optional&lt;Customer&gt; customer = CustomerService.getCustomerById(1);\n        if (customer.isPresent()) {\n            System.out.println(\"\u627e\u5230\u5ba2\u6237\uff1a\" + customer.get().getName());\n        } else {\n            System.out.println(\"\u627e\u4e0d\u5230\u5ba2\u6237\");\n        }\n    }\n}<\/pre>\n<p>\u901a\u8fc7\u4f7f\u7528 <code>Optional<\/code> \u7c7b\uff0c\u6211\u4eec\u907f\u514d\u4e86 <code>NullPointerException<\/code>\uff0c\u5e76\u4f7f\u4ee3\u7801\u66f4\u6613\u4e8e\u7ef4\u62a4\u548c\u7406\u89e3\u3002\u5176\u4ed6\u9519\u8bef\u5904\u7406\u7b56\u7565\uff0c\u5982 <code>Result<\/code> \u548c <code>Either<\/code>\uff0c\u53ef\u4ee5\u5728\u66f4\u590d\u6742\u7684\u60c5\u51b5\u4e0b\u63d0\u4f9b\u66f4\u5927\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aJava \u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u7684\u9519\u8bef\u5904\u7406\u7b56\u7565\u6709\u54ea\u4e9b\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u7684\u9519\u8bef\u5904\u7406&#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-202615","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202615","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=202615"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202615\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}