{"id":202997,"date":"2025-05-22T09:08:19","date_gmt":"2025-05-22T01:08:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202997\/"},"modified":"2025-05-22T09:08:19","modified_gmt":"2025-05-22T01:08:19","slug":"java%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e4%b8%8e%e5%a0%86%e6%a0%88%e8%b7%9f%e8%b8%aa","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202997\/","title":{"rendered":"Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u5f02\u5e38\u5904\u7406\u4e0e\u5806\u6808\u8ddf\u8e2a"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u5f02\u5e38\u5904\u7406\u4e0e\u5806\u6808\u8ddf\u8e2a<\/h1>\n<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300aJava\u51fd\u6570\u5f0f\u7f16\u7a0b\u5f02\u5e38\u5904\u7406\u4e0e\u5806\u6808\u8ddf\u8e2a\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01<\/p>\n<p>\u5728 Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u53ef\u901a\u8fc7\u4e24\u79cd\u65b9\u5f0f\u8fdb\u884c\uff1aChecked Exceptions\u901a\u8fc7 try-catch \u5757\u6216\u629b\u51fa\u5f02\u5e38\u5904\u7406\uff0c\u800cUnchecked Exceptions\u53ef\u901a\u8fc7 Either \u6216 Try \u7b49\u7c7b\u578b\u5904\u7406\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5f02\u5e38\u4e0d\u5305\u542b\u5806\u6808\u8ddf\u8e2a\uff0c\u4f46\u53ef\u4f7f\u7528 Throwable.getStackTrace() \u65b9\u6cd5\u6dfb\u52a0\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730015869671df27da2474.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u7684\u5f02\u5e38\u5904\u7406\u4e0e\u5806\u6808\u8ddf\u8e2a<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 Java 8 \u53ca\u66f4\u9ad8\u7248\u672c\u8fdb\u884c\u51fd\u6570\u5f0f\u7f16\u7a0b\u65f6\uff0c\u5904\u7406\u5f02\u5e38\u81f3\u5173\u91cd\u8981\u3002\u5728\u629b\u51fa\u5f02\u5e38\u7684\u60c5\u51b5\u4e0b\uff0c\u4e86\u89e3\u5f02\u5e38\u7684\u6839\u6e90\u5e76\u6536\u96c6\u5806\u6808\u8ddf\u8e2a\u5bf9\u4e8e\u8c03\u8bd5\u548c\u89e3\u51b3\u95ee\u9898\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p><strong>\u5904\u7406\u5f02\u5e38<\/strong><\/p>\n<p>Java \u63d0\u4f9b\u4e86\u4e24\u79cd\u4e0d\u540c\u7684\u65b9\u5f0f\u6765\u5904\u7406\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u7684\u5f02\u5e38\uff1a<\/p>\n<ul>\n<li><strong>Checked Exceptions:<\/strong> \u8fd9\u4e9b\u5f02\u5e38\u8868\u793a\u7f16\u8bd1\u671f\u95f4\u65e0\u6cd5\u68c0\u6d4b\u5230\u7684\u9519\u8bef\uff0c\u4f8b\u5982\u6587\u4ef6\u7cfb\u7edf\u64cd\u4f5c\u6216\u6570\u636e\u5e93\u8fde\u63a5\u5931\u8d25\u3002\u5b83\u4eec\u9700\u8981\u663e\u5f0f\u5904\u7406\uff0c\u8981\u4e48\u7528 <code>try-catch<\/code> \u5757\uff0c\u8981\u4e48\u629b\u51fa\u5f02\u5e38\u3002<\/li>\n<li><strong>Unchecked Exceptions:<\/strong> \u8fd9\u4e9b\u5f02\u5e38\u8868\u793a\u7f16\u8bd1\u671f\u95f4\u53ef\u4ee5\u68c0\u6d4b\u5230\u7684\u9519\u8bef\uff0c\u4f8b\u5982 <code>NullPointerException<\/code> \u6216 <code>ArrayIndexOutOfBoundsException<\/code>\u3002\u5728\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\uff0c\u5b83\u4eec\u53ef\u4ee5\u901a\u8fc7 <code>Either<\/code> \u6216 <code>Try<\/code> \u7b49\u7c7b\u578b\u8fdb\u884c\u5904\u7406\u3002<\/li>\n<\/ul>\n<p><strong>\u6dfb\u52a0\u5806\u6808\u8ddf\u8e2a<\/strong><\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u7684\u5f02\u5e38\u4e0d\u4f1a\u5305\u542b\u5806\u6808\u8ddf\u8e2a\u4fe1\u606f\u3002\u4e3a\u4e86\u5728\u5904\u7406\u5f02\u5e38\u65f6\u83b7\u5f97\u5806\u6808\u8ddf\u8e2a\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>Throwable.getStackTrace()<\/code> \u65b9\u6cd5\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a\u4f7f\u7528 <code>Either<\/code> \u5904\u7406\u5f02\u5e38<\/strong><\/p>\n<pre>import java.io.FileNotFoundException;\nimport java.util.Optional;\n\npublic class EitherExample {\n\n    public static Either&lt;FileNotFoundException, String&gt; readFile(String filename) {\n        try {\n            return Either.right(readFileContent(filename));\n        } catch (FileNotFoundException e) {\n            return Either.left(e);\n        }\n    }\n\n    private static String readFileContent(String filename) {\n        \/\/ \u8bfb\u6587\u4ef6\u5e76\u8fd4\u56de\u5176\u5185\u5bb9\n        return \"\u6587\u4ef6\u5185\u5bb9\";\n    }\n\n    public static void main(String[] args) {\n        Either&lt;FileNotFoundException, String&gt; result = readFile(\"file.txt\");\n\n        result.fold(\n            error -&gt; System.out.println(\"\u9519\u8bef\uff1a\" + error.getStackTrace()[0]),\n            content -&gt; System.out.println(\"\u5185\u5bb9\uff1a\" + content)\n        );\n    }\n}<\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>readFile<\/code> \u51fd\u6570\u8fd4\u56de\u4e00\u4e2a <code>Either<\/code>\uff0c\u5176\u4e2d\u5305\u542b\u6587\u4ef6\u5185\u5bb9\u6216 <code>FileNotFoundException<\/code>\u3002\u6211\u4eec\u4f7f\u7528 <code>fold<\/code> \u51fd\u6570\u5904\u7406 <code>Either<\/code>\uff0c\u5b83\u6839\u636e\u5176\u7c7b\u578b\u8c03\u7528\u4e0d\u540c\u7684\u5904\u7406\u51fd\u6570\u3002\u5bf9\u4e8e <code>FileNotFoundException<\/code>\uff0c\u6211\u4eec\u6253\u5370\u5806\u6808\u8ddf\u8e2a\u7684\u7b2c\u4e00\u884c\u3002<\/p>\n<p><strong>\u4f7f\u7528 <code>Try<\/code> \u5904\u7406\u5f02\u5e38<\/strong><\/p>\n<pre>import java.io.FileNotFoundException;\n\npublic class TryExample {\n\n    public static Try&lt;String&gt; readFile(String filename) {\n        try {\n            return Try.of(readFileContent(filename));\n        } catch (FileNotFoundException e) {\n            return Try.failure(e);\n        }\n    }\n\n    \/\/ ... \u5176\u4ed6\u4ee3\u7801 ...\n\n}<\/pre>\n<p><code>Try<\/code> \u7c7b\u4f3c\u4e8e <code>Either<\/code>\uff0c\u4f46\u5b83\u53ea\u8868\u793a\u6210\u529f\u6216\u5931\u8d25\uff08\u6ca1\u6709\u503c\uff09\u3002\u53ef\u4ee5\u901a\u8fc7 <code>isSuccess()<\/code> \u548c <code>isFailure()<\/code> \u65b9\u6cd5\u68c0\u67e5\u5176\u72b6\u6001\uff0c\u5e76\u4f7f\u7528 <code>get()<\/code> \u6216 <code>orElse()<\/code> \u65b9\u6cd5\u83b7\u53d6\u5185\u5bb9\u6216\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aJava\u51fd\u6570\u5f0f\u7f16\u7a0b\u5f02\u5e38\u5904\u7406\u4e0e\u5806\u6808\u8ddf\u8e2a\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u51fd\u6570\u5f0f\u7f16\u7a0b,\u5f02\u5e38\u5904\u7406\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u5f02\u5e38\u5904\u7406\u4e0e\u5806\u6808&#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-202997","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202997","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=202997"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202997\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202997"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202997"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202997"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}