{"id":202815,"date":"2025-05-22T10:24:20","date_gmt":"2025-05-22T02:24:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202815\/"},"modified":"2025-05-22T10:24:20","modified_gmt":"2025-05-22T02:24:20","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-try-catch-finally-%e5%9d%97%e5%a4%84%e7%90%86%e5%bc%82%e5%b8%b8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202815\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 try-catch-finally \u5757\u5904\u7406\u5f02\u5e38\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 try-catch-finally \u5757\u5904\u7406\u5f02\u5e38\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300a\u5982\u4f55\u4f7f\u7528 try-catch-finally \u5757\u5904\u7406\u5f02\u5e38\uff1f\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~<\/p>\n<p>try-catch-finally \u5757\u5904\u7406\u5f02\u5e38\uff0c\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u7a33\u5b9a\u6027\uff1atry \u5757\u5305\u542b\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\u3002catch \u5757\u6355\u83b7\u7279\u5b9a\u5f02\u5e38\u5e76\u5904\u7406\u5b83\u3002finally \u5757\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0c\u59cb\u7ec8\u6267\u884c\u4ee3\u7801\uff0c\u7528\u4e8e\u91ca\u653e\u8d44\u6e90\u6216\u6e05\u7406\u64cd\u4f5c\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729945044671cddd49aeb4.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u4f7f\u7528 try-catch-finally \u5757\u5904\u7406\u5f02\u5e38<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p><code>try-catch-finally<\/code> \u5757\u662f\u4e00\u79cd\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u7528\u4e8e\u6355\u83b7\u548c\u5904\u7406\u7a0b\u5e8f\u4e2d\u7684\u5f02\u5e38\u3002\u5b83\u53ef\u4ee5\u5728\u4fdd\u6301\u4ee3\u7801\u6574\u6d01\u548c\u53ef\u63a7\u7684\u540c\u65f6\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<pre>try {\n  \/\/ \u53ef\u80fd\u4f1a\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\n} catch (Exception exceptionName) {\n  \/\/ \u6355\u83b7\u5f02\u5e38\u5e76\u5904\u7406\u5b83\n} finally {\n  \/\/ \u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0c\u59cb\u7ec8\u6267\u884c\u7684\u4ee3\u7801\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u51fd\u6570 <code>divide()<\/code>\uff0c\u5b83\u5c1d\u8bd5\u5c06\u4e24\u4e2a\u6570\u5b57\u76f8\u9664\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528 <code>try-catch-finally<\/code> \u5757\u5904\u7406\u6f5c\u5728\u7684 <code>ArithmeticException<\/code> \u5f02\u5e38\uff1a<\/p>\n<pre>public static void divide(int a, int b) {\n  try {\n    int result = a \/ b;\n    System.out.println(\"Result: \" + result);\n  } catch (ArithmeticException e) {\n    System.out.println(\"Error: Division by zero\");\n  } finally {\n    System.out.println(\"Division operation complete\");\n  }\n}<\/pre>\n<p>\u5f53\u8c03\u7528 <code>divide(10, 2)<\/code> \u65f6\uff0c\u7a0b\u5e8f\u5c06\u8f93\u51fa\uff1a<\/p>\n<pre>Result: 5\nDivision operation complete<\/pre>\n<p>\u5982\u679c\u8c03\u7528 <code>divide(10, 0)<\/code> \u65f6\uff0c\u7a0b\u5e8f\u5c06\u8f93\u51fa\uff1a<\/p>\n<pre>Error: Division by zero\nDivision operation complete<\/pre>\n<p><strong>finally \u5757<\/strong><\/p>\n<p>\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0c<code>finally<\/code> \u5757\u4e2d\u7684\u4ee3\u7801\u59cb\u7ec8\u4f1a\u88ab\u6267\u884c\u3002\u8fd9\u53ef\u4ee5\u7528\u4e8e\u91ca\u653e\u8d44\u6e90\uff08\u4f8b\u5982\u6587\u4ef6\u53e5\u67c4\u6216\u6570\u636e\u5e93\u8fde\u63a5\uff09\u6216\u6267\u884c\u4efb\u4f55\u5176\u4ed6\u5fc5\u9700\u7684\u6e05\u7406\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>\u6355\u83b7\u5c3d\u53ef\u80fd\u7279\u5b9a\u7684\u5f02\u5e38\u7c7b\u578b\u3002<\/li>\n<li>\u7f16\u5199\u6e05\u6670\u3001\u7b80\u6d01\u7684\u5f02\u5e38\u5904\u7406\u4ee3\u7801\uff0c\u4f7f\u5176\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/li>\n<li>\u8003\u8651\u4f7f\u7528\u65e5\u5fd7\u6216\u5176\u4ed6\u65b9\u6cd5\u6765\u8bb0\u5f55\u6355\u83b7\u7684\u5f02\u5e38\u3002<\/li>\n<li>\u786e\u4fdd\u5728 <code>finally<\/code> \u5757\u4e2d\u91ca\u653e\u6240\u6709\u8d44\u6e90\uff0c\u4ee5\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u3002<\/li>\n<\/ul>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528 try-catch-finally \u5757\u5904\u7406\u5f02\u5e38\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 try-catch-f&#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-202815","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202815","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=202815"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202815\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}