{"id":256218,"date":"2024-05-15T11:39:47","date_gmt":"2024-05-15T03:39:47","guid":{"rendered":""},"modified":"2024-05-15T11:39:54","modified_gmt":"2024-05-15T03:39:54","slug":"%e5%a6%82%e4%bd%95%e6%8d%95%e8%8e%b7reactor%e4%bb%a3%e7%a0%81%e6%8a%9b%e5%87%ba%e7%9a%84%e5%bc%82%e5%b8%b8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/256218.html","title":{"rendered":"\u5982\u4f55\u6355\u83b7reactor\u4ee3\u7801\u629b\u51fa\u7684\u5f02\u5e38"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27102047\/937bc533-48dc-4118-bc3c-0a8c21bb219e.webp\" alt=\"\u5982\u4f55\u6355\u83b7reactor\u4ee3\u7801\u629b\u51fa\u7684\u5f02\u5e38\" \/><\/p>\n<p><p>\u6355\u83b7Reactor\u4ee3\u7801\u629b\u51fa\u7684\u5f02\u5e38\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5176\u4e2d\u5305\u62ec\u4f7f\u7528<code>onErrorReturn()<\/code><strong>\u3001<\/strong><code>onErrorResume()<\/code><strong>\u3001<\/strong><code>doOnError()<\/code> \u65b9\u6cd5\uff0c\u4ee5\u53ca <strong><code>try-catch<\/code><\/strong> \u5757 \u7ed3\u5408 <strong><code>Mono<\/code>\/<code>Flux<\/code><\/strong> \u4f7f\u7528\u3002\u5f53\u5728Reactor\u6d41\u7a0b\u4e2d\u9047\u5230\u5f02\u5e38\u65f6\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u5730\u6355\u83b7\u5e76\u5904\u7406\u5f02\u5e38\u3002\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5b83\u4eec\u5206\u522b\u5982\u4f55\u8fd0\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c<strong><code>onErrorResume()<\/code><\/strong> \u65b9\u6cd5\u80fd\u591f\u6355\u83b7\u5f02\u5e38\u5e76\u8fd4\u56de\u4e00\u4e2a\u5907\u7528\u7684\u54cd\u5e94\u5e8f\u5217\uff0c\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5904\u7406\u9519\u8bef\u548c\u63d0\u4f9b\u5907\u9009\u903b\u8f91\u7684\u673a\u4f1a\u3002\u8fd9\u4f7f\u5f97\u6211\u4eec\u80fd\u591f\u786e\u4fdd\u5373\u4f7f\u5728\u51fa\u73b0\u9519\u8bef\u7684\u60c5\u51b5\u4e0b\uff0c\u5e94\u7528\u7a0b\u5e8f\u4f9d\u7136\u53ef\u4ee5\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\u6216\u63d0\u4f9b\u9ed8\u8ba4\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 <code>ONERRORRETURN()<\/code><\/h3>\n<\/p>\n<p><p><code>onErrorReturn()<\/code> \u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u6355\u83b7\u5f02\u5e38\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u9759\u6001\u7684\u9ed8\u8ba4\u503c\u3002\u5f53\u6d41\u4e2d\u629b\u51fa\u9519\u8bef\u65f6\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u8ba9\u6d41\u7ec8\u6b62\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u9884\u8bbe\u7684\u503c\u4f5c\u4e3a\u66ff\u4ee3\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>\u63d0\u4f9b\u9759\u6001\u9ed8\u8ba4\u503c<\/h4>\n<\/p>\n<p><p>\u5f53\u5f02\u5e38\u53d1\u751f\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u60f3\u8981\u8fd4\u56de\u4e00\u4e2a\u9759\u6001\u7684\u9ed8\u8ba4\u503c\u3002\u8fd9\u5728\u9700\u8981\u4fdd\u6301\u6d41\u7684\u7ee7\u7eed\u8fd0\u884c\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">Flux&lt;String&gt; flux = Flux.just(&quot;key1&quot;, &quot;key2&quot;)<\/p>\n<p>                        .map(key -&gt; {<\/p>\n<p>                            if (&quot;error&quot;.equals(key)) {<\/p>\n<p>                                throw new RuntimeException(&quot;Error Occurred&quot;);<\/p>\n<p>                            }<\/p>\n<p>                            return &quot;Value for &quot; + key;<\/p>\n<p>                        })<\/p>\n<p>                        .onErrorReturn(&quot;Default Value&quot;);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5982\u679c\u6d41\u4e2d\u7684\u4efb\u4f55\u5143\u7d20\u89e6\u53d1\u5f02\u5e38\uff0c\u5219\u4f1a\u7acb\u5373\u8fd4\u56de\u9ed8\u8ba4\u503c\u201cDefault Value\u201d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 <code>ONERRORRESUME()<\/code><\/h3>\n<\/p>\n<p><p><code>onErrorResume()<\/code> \u65b9\u6cd5\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\u3002\u5b83\u80fd\u591f\u6355\u83b7\u5f02\u5e38\uff0c\u5e76\u5141\u8bb8\u6211\u4eec\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684<code>Publisher<\/code>\uff0c\u4ece\u800c\u63d0\u4f9b\u66f4\u591a\u7684\u9519\u8bef\u6062\u590d\u9009\u9879\u3002<\/p>\n<\/p>\n<p><h4>\u63d0\u4f9b\u5907\u7528\u903b\u8f91<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>onErrorResume()<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u5f02\u5e38\u7c7b\u578b\u63d0\u4f9b\u4e0d\u540c\u7684\u5907\u7528\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">Flux&lt;String&gt; flux = Flux.just(&quot;key1&quot;, &quot;key2&quot;)<\/p>\n<p>                        .flatMap(key -&gt; {<\/p>\n<p>                            if (&quot;error&quot;.equals(key)) {<\/p>\n<p>                                throw new RuntimeException(&quot;Error Occurred&quot;);<\/p>\n<p>                            }<\/p>\n<p>                            return Mono.just(&quot;Processed &quot; + key);<\/p>\n<p>                        })<\/p>\n<p>                        .onErrorResume(e -&gt; {<\/p>\n<p>                            if (e instanceof RuntimeException) {<\/p>\n<p>                                return Mono.just(&quot;Runtime Exception Occurred&quot;);<\/p>\n<p>                            }<\/p>\n<p>                            return Mono.just(&quot;Other Exception Occurred&quot;);<\/p>\n<p>                        });<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u6839\u636e\u5f02\u5e38\u7684\u7c7b\u578b\u8fd4\u56de\u4e0d\u540c\u7684\u54cd\u5e94\u5e8f\u5217\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528 <code>DOONERROR()<\/code><\/h3>\n<\/p>\n<p><p><code>doOnError()<\/code> \u65b9\u6cd5\u4e0d\u4f1a\u6539\u53d8\u6d41\u7684\u6570\u636e\u5e8f\u5217\uff0c\u4f46\u662f\u5b83\u5141\u8bb8\u6211\u4eec\u5bf9\u9519\u8bef\u4e8b\u4ef6\u8fdb\u884c\u4fa7\u9762\u7684\u5904\u7406\uff0c\u4f8b\u5982\u8bb0\u5f55\u65e5\u5fd7\u3002\u4f7f\u7528\u8be5\u65b9\u6cd5\u5e76\u4e0d\u4f1a\u5904\u7406\u5f02\u5e38\uff0c\u53ea\u662f\u4e3a\u5f02\u5e38\u63d0\u4f9b\u4e86\u4e00\u4e2a\u56de\u8c03\u3002<\/p>\n<\/p>\n<p><h4>\u9519\u8bef\u65e5\u5fd7\u8bb0\u5f55<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5728<code>doOnError()<\/code>\u4e2d\u8fdb\u884c\u5f02\u5e38\u65e5\u5fd7\u8bb0\u5f55\uff0c\u800c\u4e0d\u5f71\u54cd\u6d41\u7684\u5904\u7406\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">Flux&lt;String&gt; flux = Flux.just(&quot;key1&quot;, &quot;key2&quot;)<\/p>\n<p>                        .map(key -&gt; {<\/p>\n<p>                            if (&quot;error&quot;.equals(key)) {<\/p>\n<p>                                throw new RuntimeException(&quot;Error Occurred&quot;);<\/p>\n<p>                            }<\/p>\n<p>                            return &quot;Value for &quot; + key;<\/p>\n<p>                        })<\/p>\n<p>                        .doOnError(e -&gt; {<\/p>\n<p>                            System.err.println(&quot;The error is: &quot; + e.getMessage());<\/p>\n<p>                        });<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>doOnError()<\/code>\u8bb0\u5f55\u9519\u8bef\uff0c\u4e0d\u4f1a\u4f7f\u6d41\u4e2d\u65ad\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001TRY-CATCH \u7ed3\u5408 <code>MONO<\/code>\/<code>FLUX<\/code><\/h3>\n<\/p>\n<p><p>\u867d\u7136\u5728\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u4e0d\u9f13\u52b1\u4f7f\u7528<code>try-catch<\/code>\uff0c\u4f46\u5728\u7279\u5b9a\u60c5\u51b5\u4e0b\uff0c\u5b83\u4ecd\u7136\u53ef\u4ee5\u7528\u6765\u540c\u6b65\u6355\u83b7\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h4>\u540c\u6b65\u4ee3\u7801\u4e2d\u6355\u83b7\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u5f53\u8c03\u7528\u540c\u6b65\u4ee3\u7801\u6216\u963b\u585e\u64cd\u4f5c\u65f6\uff0c<code>try-catch<\/code>\u5757\u53ef\u4ee5\u76f4\u63a5\u5728\u8c03\u7528\u70b9\u6355\u83b7\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">Flux&lt;String&gt; flux = Flux.just(&quot;key1&quot;, &quot;key2&quot;)<\/p>\n<p>                        .flatMap(key -&gt; {<\/p>\n<p>                            try {<\/p>\n<p>                                if (&quot;error&quot;.equals(key)) {<\/p>\n<p>                                    throw new RuntimeException(&quot;Error Occurred&quot;);<\/p>\n<p>                                }<\/p>\n<p>                                return Mono.just(&quot;Processed &quot; + key);<\/p>\n<p>                            } catch (Exception e) {<\/p>\n<p>                                return Mono.error(e);<\/p>\n<p>                            }<\/p>\n<p>                        });<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528\u4e86<code>try-catch<\/code>\u6765\u540c\u6b65\u6355\u83b7\u5e76\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><p>\u603b\u800c\u8a00\u4e4b\uff0cReactor\u63d0\u4f9b\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u58f0\u660e\u5f0f\u7f16\u7a0b\u8303\u5f0f\u4e0b\u4f18\u96c5\u5730\u5904\u7406\u5f02\u5e38\u3002\u4e0d\u540c\u7684\u9519\u8bef\u5904\u7406\u64cd\u4f5c\u7b26\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u4e1a\u52a1\u9700\u8981\u548c\u5f02\u5e38\u5904\u7406\u7b56\u7565\uff0c\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u5f0f\u6765\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5373\u4f7f\u5728\u53cd\u5e94\u5f0f\u6d41\u4e2d\u9047\u5230\u9519\u8bef\u60c5\u51b5\uff0c\u6211\u4eec\u4e5f\u80fd\u4fdd\u8bc1\u5e94\u7528\u7684\u5065\u58ee\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1.\u4e3a\u4ec0\u4e48\u6211\u7684reactor\u4ee3\u7801\u4f1a\u629b\u51fa\u5f02\u5e38\uff1f<\/strong><\/p>\n<p>\u5f02\u5e38\u5728reactor\u4ee3\u7801\u4e2d\u7684\u51fa\u73b0\u53ef\u80fd\u662f\u7531\u4e8e\u591a\u79cd\u539f\u56e0\u5f15\u8d77\u7684\u3002\u53ef\u80fd\u662f\u7531\u4e8e\u9519\u8bef\u7684\u8f93\u5165\u3001\u672a\u5904\u7406\u7684\u8fb9\u754c\u60c5\u51b5\u3001\u9519\u8bef\u7684\u914d\u7f6e\u6216\u9519\u8bef\u7684\u4ee3\u7801\u903b\u8f91\u7b49\u3002\u5728\u6355\u83b7\u5f02\u5e38\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u4e86\u89e3\u5f02\u5e38\u51fa\u73b0\u7684\u539f\u56e0\u3002<\/p>\n<p><strong>2.\u5982\u4f55\u6355\u83b7reactor\u4ee3\u7801\u629b\u51fa\u7684\u5f02\u5e38\uff1f<\/strong><\/p>\n<p>\u6355\u83b7reactor\u4ee3\u7801\u629b\u51fa\u7684\u5f02\u5e38\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<ul>\n<li>\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528try-catch\u5757\u6765\u6355\u83b7\u5f02\u5e38\u3002\u5c06\u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801\u653e\u5728try\u5757\u4e2d\uff0c\u7136\u540e\u5728catch\u5757\u4e2d\u5904\u7406\u5f02\u5e38\u3002<\/li>\n<li>\u6839\u636e\u5f02\u5e38\u7684\u7c7b\u578b\uff0c\u9009\u62e9\u9002\u5f53\u7684\u5f02\u5e38\u5904\u7406\u7b56\u7565\u3002\u53ef\u4ee5\u9009\u62e9\u91cd\u65b0\u5c1d\u8bd5\u64cd\u4f5c\u3001\u8fd4\u56de\u9519\u8bef\u4fe1\u606f\u3001\u8bb0\u5f55\u5f02\u5e38\u65e5\u5fd7\u7b49\u5904\u7406\u65b9\u5f0f\u3002<\/li>\n<li>\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u5668\u6765\u8ddf\u8e2a\u548c\u5904\u7406\u5f02\u5e38\u3002\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406\u5668\u6765\u7edf\u4e00\u5904\u7406\u5f02\u5e38\uff0c\u786e\u4fdd\u4ee3\u7801\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/li>\n<li>\u4f7f\u7528reactor\u63d0\u4f9b\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\u3002reactor\u6846\u67b6\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u7684\u9519\u8bef\u5904\u7406\u64cd\u4f5c\u7b26\uff0c\u4f8b\u5982<code>onErrorReturn<\/code>\u3001<code>onErrorResume<\/code>\u7b49\uff0c\u53ef\u4ee5\u7528\u6765\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\u3002<\/li>\n<\/ul>\n<p><strong>3.\u5982\u4f55\u9884\u9632reactor\u4ee3\u7801\u51fa\u73b0\u5f02\u5e38\uff1f<\/strong><\/p>\n<p>\u9664\u4e86\u6355\u83b7\u5f02\u5e38\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u91c7\u53d6\u4e00\u4e9b\u9884\u9632\u63aa\u65bd\u6765\u907f\u514dreactor\u4ee3\u7801\u51fa\u73b0\u5f02\u5e38\uff1a<\/p>\n<ul>\n<li>\u5bf9\u8f93\u5165\u8fdb\u884c\u9a8c\u8bc1\u548c\u8fc7\u6ee4\uff0c\u786e\u4fdd\u8f93\u5165\u7684\u5408\u6cd5\u6027\u548c\u6709\u6548\u6027\u3002<\/li>\n<li>\u4f7f\u7528\u5408\u9002\u7684\u8fb9\u754c\u6761\u4ef6\u8fdb\u884c\u9632\u5fa1\u6027\u7f16\u7a0b\uff0c\u5904\u7406\u7279\u6b8a\u60c5\u51b5\u3002<\/li>\n<li>\u5bf9\u4ee3\u7801\u8fdb\u884c\u4e25\u683c\u7684\u6d4b\u8bd5\u548c\u8d28\u91cf\u63a7\u5236\uff0c\u786e\u4fdd\u4ee3\u7801\u7684\u53ef\u9760\u6027\u548c\u7a33\u5b9a\u6027\u3002<\/li>\n<li>\u4f7f\u7528\u5408\u9002\u7684\u914d\u7f6e\u548c\u53c2\u6570\uff0c\u907f\u514d\u51fa\u73b0\u4e0d\u5fc5\u8981\u7684\u5f02\u5e38\u3002<\/li>\n<li>\u4fdd\u6301\u4ee3\u7801\u7684\u7b80\u6d01\u548c\u53ef\u8bfb\u6027\uff0c\u51cf\u5c11\u51fa\u73b0\u9519\u8bef\u7684\u53ef\u80fd\u6027\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"\u6355\u83b7Reactor\u4ee3\u7801\u629b\u51fa\u7684\u5f02\u5e38\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5176\u4e2d\u5305\u62ec\u4f7f\u7528onErrorReturn()\u3001onErro [&hellip;]","protected":false},"author":3,"featured_media":256225,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/256218"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=256218"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/256218\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/256225"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=256218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=256218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=256218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}