{"id":202791,"date":"2025-05-22T10:41:41","date_gmt":"2025-05-22T02:41:41","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202791\/"},"modified":"2025-05-22T10:41:41","modified_gmt":"2025-05-22T02:41:41","slug":"%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e5%a6%82%e4%bd%95%e5%bd%b1%e5%93%8d-java-%e5%87%bd%e6%95%b0%e6%89%a7%e8%a1%8c%e6%95%88%e7%8e%87%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202791\/","title":{"rendered":"\u5f02\u5e38\u5904\u7406\u5982\u4f55\u5f71\u54cd Java \u51fd\u6570\u6267\u884c\u6548\u7387\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5f02\u5e38\u5904\u7406\u5982\u4f55\u5f71\u54cd Java \u51fd\u6570\u6267\u884c\u6548\u7387\uff1f<\/h1>\n<p>\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5f02\u5e38\u5904\u7406\u5982\u4f55\u5f71\u54cd Java \u51fd\u6570\u6267\u884c\u6548\u7387\uff1f\u300b<\/span>\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span><\/span>\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01<\/p>\n<p>\u5f02\u5e38\u5904\u7406\u4f1a\u5f71\u54cd Java \u51fd\u6570\u6267\u884c\u6548\u7387\uff0c\u539f\u56e0\u5982\u4e0b\uff1a\u5f02\u5e38\u7c7b\u578b\uff1a\u68c0\u67e5\u5f02\u5e38\u5f00\u9500\u66f4\u5927\u3002\u5904\u7406\u4f4d\u7f6e\uff1a\u5728\u51fd\u6570\u5f00\u5934\u5904\u7406\u5f02\u5e38\u66f4\u6709\u6548\u7387\u3002\u5904\u7406\u8bed\u53e5\u6570\u91cf\uff1a\u591a\u4e2a try-catch \u5757\u4f1a\u589e\u52a0\u5f00\u9500\u3002\u6d4b\u8bd5\u8868\u660e\uff0c\u5f02\u5e38\u5904\u7406\u589e\u52a0\u4e86\u7ea6 7% \u7684\u6267\u884c\u65f6\u95f4\u3002\u6700\u4f73\u5b9e\u8df5\u5305\u62ec\u4ec5\u5904\u7406\u5fc5\u9700\u5f02\u5e38\u3001\u5728\u51fd\u6570\u5f00\u5934\u5904\u7406\u5f02\u5e38\u4ee5\u53ca\u5728\u6027\u80fd\u5173\u952e\u64cd\u4f5c\u4e2d\u4f7f\u7528\u672a\u68c0\u67e5\u5f02\u5e38\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729942470671cd3c69686d.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5f02\u5e38\u5904\u7406\u5bf9 Java \u51fd\u6570\u6267\u884c\u6548\u7387\u7684\u5f71\u54cd<\/strong><\/p>\n<p>\u5f02\u5e38\u5904\u7406\u662f\u5904\u7406\u8fd0\u884c\u65f6\u9519\u8bef\u548c\u5f02\u5e38\u7684\u5173\u952e\u673a\u5236\u3002\u7136\u800c\uff0c\u5b83\u4e5f\u4f1a\u5bf9\u51fd\u6570\u7684\u6267\u884c\u6548\u7387\u4ea7\u751f\u5f71\u54cd\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u989d\u5916\u7684\u5f00\u9500\u6765\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<p><strong>\u5f71\u54cd\u56e0\u7d20<\/strong><\/p>\n<p>\u5f71\u54cd\u5f02\u5e38\u5904\u7406\u6548\u7387\u7684\u56e0\u7d20\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u5f02\u5e38\u7c7b\u578b\uff1a<\/strong>\u68c0\u67e5\u5f02\u5e38\u9700\u8981\u66f4\u591a\u7684\u5f00\u9500\uff0c\u800c\u672a\u68c0\u67e5\u5f02\u5e38\u5219\u76f8\u53cd\u3002<\/li>\n<li><strong>\u5f02\u5e38\u5904\u7406\u4f4d\u7f6e\uff1a<\/strong>\u5728\u51fd\u6570\u5f00\u5934\u68c0\u67e5\u5f02\u5e38\u6bd4\u5728\u51fd\u6570\u5185\u90e8\u68c0\u67e5\u66f4\u6709\u5229\u3002<\/li>\n<li><strong>\u5f02\u5e38\u5904\u7406\u8bed\u53e5\u6570\u91cf\uff1a<\/strong>\u4f7f\u7528\u591a\u4e2a <code>try-catch<\/code> \u5757\u5904\u7406\u4e0d\u540c\u7684\u5f02\u5e38\u5c06\u5bfc\u81f4\u66f4\u591a\u7684\u5f00\u9500\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b\u4e24\u4e2a\u51fd\u6570\uff1a<\/p>\n<pre>\/\/ Function without exception handling\npublic int divide(int a, int b) {\n    return a \/ b;\n}\n\n\/\/ Function with exception handling\npublic int divideWithExceptionHandling(int a, int b) {\n    try {\n        return a \/ b;\n    } catch (ArithmeticException e) {\n        return 0; \/\/ Handle division by zero\n    }\n}<\/pre>\n<p>\u5728\u6ca1\u6709\u5f02\u5e38\u5904\u7406\u7684\u60c5\u51b5\u4e0b\uff0c<code>divide<\/code> \u51fd\u6570\u5728\u9047\u5230\u9664\u4ee5\u96f6\u9519\u8bef\u65f6\u4f1a\u629b\u51fa <code>ArithmeticException<\/code> \u5e76\u7ec8\u6b62\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c<code>divideWithExceptionHandling<\/code> \u51fd\u6570\u6355\u83b7\u5f02\u5e38\u5e76\u8fd4\u56de 0\u3002<\/p>\n<p><strong>\u6027\u80fd\u5dee\u5f02<\/strong><\/p>\n<p>\u4f7f\u7528 JMH \u57fa\u51c6\u6d4b\u8bd5\u5de5\u5177\u6d4b\u8bd5\u8fd9\u4e9b\u51fd\u6570\uff0c\u7ed3\u679c\u663e\u793a\uff0c\u5728 100 \u4e07\u6b21\u8fed\u4ee3\u4e2d\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u51fd\u6570<\/th>\n<th>\u6267\u884c\u65f6\u95f4 (ms)<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>divide<\/td>\n<td>210<\/td>\n<\/tr>\n<tr>\n<td>divideWithExceptionHandling<\/td>\n<td>225<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u53ef\u4ee5\u770b\u51fa\uff0c\u5f02\u5e38\u5904\u7406\u589e\u52a0\u4e86\u5927\u7ea6 7% \u7684\u6267\u884c\u65f6\u95f4\u3002<\/p>\n<p><strong>\u6700\u4f73\u5b9e\u8df5<\/strong><\/p>\n<p>\u4e3a\u4e86\u6700\u5927\u7a0b\u5ea6\u5730\u51cf\u5c11\u5f02\u5e38\u5904\u7406\u7684\u5f00\u9500\uff0c\u8bf7\u8003\u8651\u4ee5\u4e0b\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n<ul>\n<li>\u4ec5\u5904\u7406\u90a3\u4e9b\u9700\u8981\u5728\u51fd\u6570\u4e2d\u5904\u7406\u7684\u5f02\u5e38\u3002<\/li>\n<li>\u5728\u51fd\u6570\u5f00\u5934\u68c0\u67e5\u5f02\u5e38\uff0c\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5904\u7406\u3002<\/li>\n<li>\u4f7f\u7528\u672a\u68c0\u67e5\u5f02\u5e38\u8fdb\u884c\u6027\u80fd\u5173\u952e\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u6700\u4f73\u5b9e\u8df5\uff0c\u60a8\u53ef\u4ee5\u5e73\u8861\u5f02\u5e38\u5904\u7406\u7684\u9c81\u68d2\u6027\u548c\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/p>\n<p>\u6587\u4e2d\u5173\u4e8e\u5f02\u5e38\u5904\u7406\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300a\u5f02\u5e38\u5904\u7406\u5982\u4f55\u5f71\u54cd Java \u51fd\u6570\u6267\u884c\u6548\u7387\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f02\u5e38\u5904\u7406\u5982\u4f55\u5f71\u54cd Java \u51fd\u6570&#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-202791","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202791","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=202791"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202791\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}