{"id":202991,"date":"2025-05-22T10:53:30","date_gmt":"2025-05-22T02:53:30","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202991\/"},"modified":"2025-05-22T10:53:30","modified_gmt":"2025-05-22T02:53:30","slug":"%e4%bd%bf%e7%94%a8-java-%e5%87%bd%e6%95%b0%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e7%9a%84%e5%b8%b8%e8%a7%81%e9%94%99%e8%af%af%e6%9c%89%e5%93%aa%e4%ba%9b%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202991\/","title":{"rendered":"\u4f7f\u7528 Java \u51fd\u6570\u5f02\u5e38\u5904\u7406\u7684\u5e38\u89c1\u9519\u8bef\u6709\u54ea\u4e9b\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u4f7f\u7528 Java \u51fd\u6570\u5f02\u5e38\u5904\u7406\u7684\u5e38\u89c1\u9519\u8bef\u6709\u54ea\u4e9b\uff1f<\/h1>\n<p>\u5728IT\u884c\u4e1a\u8fd9\u4e2a\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u5f88\u5feb\u7684\u884c\u4e1a\uff0c\u53ea\u6709\u4e0d\u505c\u6b62\u7684\u5b66\u4e60\uff0c\u624d\u4e0d\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u5982\u679c\u4f60\u662f\u6587\u7ae0\u5b66\u4e60\u8005\uff0c\u90a3\u4e48\u672c\u6587\u300a\u4f7f\u7528 Java \u51fd\u6570\u5f02\u5e38\u5904\u7406\u7684\u5e38\u89c1\u9519\u8bef\u6709\u54ea\u4e9b\uff1f\u300b\u5c31\u5f88\u9002\u5408\u4f60\uff01\u672c\u7bc7\u5185\u5bb9\u4e3b\u8981\u5305\u62ec##content_title##\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>Java \u51fd\u6570\u5f02\u5e38\u5904\u7406\u5b58\u5728\u4e94\u4e2a\u5e38\u89c1\u9519\u8bef\uff1a1. \u8fc7\u5ea6\u4f7f\u7528 try-catch \u5757\uff1b2. \u4e0d\u5904\u7406\u5f02\u5e38\uff1b3. \u4f7f\u7528\u5bbd\u6cdb\u7684\u5f02\u5e38\u7c7b\u578b\uff1b4. \u4e0d\u91cd\u65b0\u629b\u51fa\u5f02\u5e38\uff1b5. \u4f7f\u7528 finally \u5757\u4f5c\u4e3a\u5f02\u5e38\u5904\u7406\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730014190671debee2ad79.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>Java \u51fd\u6570\u5f02\u5e38\u5904\u7406\u7684\u5e38\u89c1\u9519\u8bef<\/strong><\/p>\n<p>\u5f02\u5e38\u5904\u7406\u5bf9\u4e8e\u5904\u7406\u9519\u8bef\u548c\u5f02\u5e38\u60c5\u51b5\u81f3\u5173\u91cd\u8981\uff0c\u4f46\u5728\u4f7f\u7528 Java \u51fd\u6570\u5f02\u5e38\u5904\u7406\u65f6\uff0c\u5b58\u5728\u4e00\u4e9b\u5e38\u89c1\u7684\u9677\u9631\u548c\u9519\u8bef\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4ee3\u7801\u6df7\u4e71\u6216\u9519\u8bef\u3002<\/p>\n<p><strong>1. \u8fc7\u5ea6\u4f7f\u7528 try-catch \u5757<\/strong><\/p>\n<p>\u6ee5\u7528 try-catch \u5757\u4f1a\u4f7f\u4ee3\u7801\u96be\u4ee5\u9605\u8bfb\u548c\u7ef4\u62a4\u3002\u5c3d\u91cf\u907f\u514d\u5c06 try-catch \u5757\u5d4c\u5957\uff0c\u5e76\u53ea\u6355\u83b7\u90a3\u4e9b\u5fc5\u987b\u5904\u7406\u7684\u5f02\u5e38\u3002<\/p>\n<p><strong>2. \u4e0d\u5904\u7406\u5f02\u5e38<\/strong><\/p>\n<p>\u6355\u83b7\u5f02\u5e38\u540e\uff0c\u5fc5\u987b\u59cb\u7ec8\u5bf9\u5176\u8fdb\u884c\u5904\u7406\u3002\u4ec5\u4ec5\u6355\u83b7\u5f02\u5e38\u800c\u4e0d\u5904\u7406\u53ef\u80fd\u4f1a\u5bfc\u81f4\u672a\u68c0\u67e5\u7684\u5f02\u5e38\u4e2d\u65ad\u5e94\u7528\u7a0b\u5e8f\u3002\u4f7f\u7528 <code>throws<\/code> \u58f0\u660e\u5c06\u5f02\u5e38\u629b\u7ed9\u8c03\u7528\u8005\uff0c\u6216\u5728 catch \u5757\u4e2d\u4f7f\u7528\u9002\u5f53\u7684\u5f02\u5e38\u5904\u7406\u3002<\/p>\n<p><strong>3. \u4f7f\u7528\u5bbd\u6cdb\u7684\u5f02\u5e38\u7c7b\u578b<\/strong><\/p>\n<p>\u907f\u514d\u6355\u83b7\u5bbd\u6cdb\u7684\u5f02\u5e38\u7c7b\u578b\uff0c\u5982 <code>Exception<\/code> \u6216 <code>Throwable<\/code>\u3002\u4f7f\u7528\u7279\u5b9a\u7684\u5f02\u5e38\u7c7b\u578b\u53ef\u4ee5\u63d0\u4f9b\u66f4\u8be6\u7ec6\u7684\u4fe1\u606f\u5e76\u542f\u7528\u66f4\u6709\u6548\u7684\u5904\u7406\u3002<\/p>\n<p><strong>4. \u4e0d\u91cd\u65b0\u629b\u51fa\u5f02\u5e38<\/strong><\/p>\n<p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6355\u83b7\u5f02\u5e38\u5e76\u91cd\u65b0\u629b\u51fa\u53ef\u80fd\u662f\u6709\u5fc5\u8981\u7684\uff0c\u4f8b\u5982\u5c06\u5176\u4f20\u64ad\u5230\u8c03\u7528\u8005\u3002\u7136\u800c\uff0c\u53ea\u91cd\u65b0\u629b\u51fa\u6355\u83b7\u7684\u5f02\u5e38\u800c\u4e0d\u6dfb\u52a0\u4efb\u4f55\u4e0a\u4e0b\u6587\u4fe1\u606f\u4f1a\u4f7f\u8c03\u8bd5\u53d8\u5f97\u56f0\u96be\u3002\u8003\u8651\u91cd\u65b0\u629b\u51fa\u5305\u542b\u66f4\u591a\u8be6\u7ec6\u4fe1\u606f\u7684\u5305\u88c5\u5f02\u5e38\u3002<\/p>\n<p><strong>5. \u4f7f\u7528 finally \u5757\u4f5c\u4e3a\u5f02\u5e38\u5904\u7406<\/strong><\/p>\n<p><code>finally<\/code> \u5757\u4e0d\u5e94\u7528\u4e8e\u5f02\u5e38\u5904\u7406\uff0c\u800c\u5e94\u4fdd\u7559\u7528\u4e8e\u6e05\u7406\u8d44\u6e90\uff0c\u4f8b\u5982\u5173\u95ed\u8fde\u63a5\u6216\u91ca\u653e\u9501\u3002\u5f02\u5e38\u5904\u7406\u5e94\u59cb\u7ec8\u5728 try-catch \u5757\u4e2d\u8fdb\u884c\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b\u51fd\u6570\uff1a<\/p>\n<pre>public static int divide(int a, int b) {\n  try {\n    return a \/ b;\n  } catch (ArithmeticException e) {\n    \/\/ \u5904\u7406\u9664\u4ee5\u96f6\u5f02\u5e38\n  }\n  return 0;\n}<\/pre>\n<p>\u6b64\u51fd\u6570\u5b58\u5728\u4ee5\u4e0b\u9519\u8bef\uff1a<\/p>\n<ul>\n<li>\u8fc7\u5ea6\u4f7f\u7528 try-catch \u5757\uff1a\u4e0d\u9700\u8981\u5916\u90e8 try-catch \u5757\uff0c\u56e0\u4e3a <code>divide()<\/code> \u6355\u83b7\u4e86 <code>ArithmeticException<\/code>\u3002<\/li>\n<li>\u4e0d\u5904\u7406\u5f02\u5e38\uff1a\u5982\u679c\u51fa\u73b0 <code>ArithmeticException<\/code>\uff0c\u4f46\u6ca1\u6709\u5904\u7406\uff0c\u51fd\u6570\u53ea\u4f1a\u8fd4\u56de 0 \u800c\u4e0d\u4f1a\u63d0\u4f9b\u4efb\u4f55\u9519\u8bef\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<p>\u4fee\u6539\u540e\u7684\u7248\u672c\uff1a<\/p>\n<pre>public static int divide(int a, int b) {\n  if (b == 0) {\n    throw new IllegalArgumentException(\"\u9664\u6570\u4e0d\u80fd\u4e3a\u96f6\");\n  }\n  return a \/ b;\n}<\/pre>\n<p>\u6b64\u7248\u672c\u4fee\u6b63\u4e86\u9519\u8bef\u5e76\u63d0\u4f9b\u4e86\u66f4\u6e05\u6670\u7684\u9519\u8bef\u5904\u7406\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u4f7f\u7528 Java \u51fd\u6570\u5f02\u5e38\u5904\u7406\u7684\u5e38\u89c1\u9519\u8bef\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>\u4f7f\u7528 Java \u51fd\u6570\u5f02\u5e38\u5904\u7406\u7684\u5e38&#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-202991","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202991","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=202991"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202991\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202991"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}