{"id":202850,"date":"2025-05-22T10:25:05","date_gmt":"2025-05-22T02:25:05","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202850\/"},"modified":"2025-05-22T10:25:05","modified_gmt":"2025-05-22T02:25:05","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-try-catch-finally-%e5%9d%97%e6%9c%89%e6%95%88%e5%9c%b0%e7%ae%a1%e7%90%86%e5%bc%82%e5%b8%b8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202850\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 try-catch-finally \u5757\u6709\u6548\u5730\u7ba1\u7406\u5f02\u5e38\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 try-catch-finally \u5757\u6709\u6548\u5730\u7ba1\u7406\u5f02\u5e38\uff1f<\/h1>\n<p><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4f7f\u7528 try-catch-finally \u5757\u6709\u6548\u5730\u7ba1\u7406\u5f02\u5e38\uff1f\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01<\/p>\n<p>try-catch-finally \u5757\u7528\u4e8e\u7ba1\u7406\u5f02\u5e38\u3002\u5b83\u5305\u542b\u4e09\u4e2a\u5757\uff1atry\uff08\u5305\u542b\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\uff09\u3001catch\uff08\u5728\u53d1\u751f\u7279\u5b9a\u5f02\u5e38\u65f6\u6267\u884c\u7684\u4ee3\u7801\uff09\u548c finally\uff08\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0c\u603b\u662f\u6267\u884c\u7684\u4ee3\u7801\uff09\uff0c\u7528\u4e8e\u91ca\u653e\u8d44\u6e90\u3002\u901a\u8fc7\u5206\u79bb\u5f02\u5e38\u5904\u7406\u548c\u6b63\u5e38\u6d41\u7a0b\uff0c\u5b83\u53ef\u589e\u5f3a\u4ee3\u7801\u53ef\u7ef4\u62a4\u6027\uff0c\u786e\u4fdd\u8d44\u6e90\u91ca\u653e\uff0c\u5e76\u4f7f\u5e94\u7528\u7a0b\u5e8f\u80fd\u591f\u4f18\u96c5\u5730\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729950875671cf49b36d04.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 try-catch-finally \u5757\u6709\u6548\u5730\u7ba1\u7406\u5f02\u5e38<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>try-catch-finally \u5757\u662f\u4e00\u79cd\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u53ef\u7528\u4e8e\u4f18\u96c5\u5730\u5904\u7406\u548c\u7ba1\u7406\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u5f02\u5e38\u3002\u5b83\u5141\u8bb8\u60a8\u5728\u53d1\u751f\u5f02\u5e38\u65f6\u6267\u884c\u7279\u5b9a\u64cd\u4f5c\uff0c\u5e76\u786e\u4fdd\u5728\u6240\u6709\u60c5\u51b5\u4e0b\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<pre>try {\n  \/\/ \u4ee3\u7801\u5757\uff0c\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\n} catch (Exception e) {\n  \/\/ \u5f53\u5f15\u53d1\u5f02\u5e38\u65f6\u6267\u884c\u7684\u4ee3\u7801\u5757\n} finally {\n  \/\/ \u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0c\u603b\u662f\u6267\u884c\u7684\u4ee3\u7801\u5757\n}<\/pre>\n<p><strong>try \u5757<\/strong><\/p>\n<p>try \u5757\u4e2d\u5305\u542b\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\u3002\u5982\u679c\u53d1\u751f\u5f02\u5e38\uff0c\u7cfb\u7edf\u4f1a\u8df3\u8f6c\u5230\u76f8\u5e94\u7684 catch \u5757\u3002<\/p>\n<p><strong>catch \u5757<\/strong><\/p>\n<p>catch \u5757\u6307\u5b9a\u5728\u53d1\u751f\u7279\u5b9a\u7c7b\u578b\u7684\u5f02\u5e38\u65f6\u8981\u6267\u884c\u7684\u4ee3\u7801\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a catch \u5757\u5904\u7406\u4e0d\u540c\u7684\u5f02\u5e38\u7c7b\u578b\u3002<\/p>\n<p><strong>finally \u5757<\/strong><\/p>\n<p>finally \u5757\u59cb\u7ec8\u6267\u884c\uff0c\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\u3002\u901a\u5e38\u7528\u4e8e\u91ca\u653e\u8d44\u6e90\uff0c\u4f8b\u5982\u5173\u95ed\u6587\u4ef6\u6216\u8fde\u63a5\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u60a8\u6709\u4e00\u4e2a\u65b9\u6cd5\u8bfb\u53d6\u5e76\u89e3\u6790\u6587\u4ef6\uff1a<\/p>\n<pre>public static void readFile(String path) {\n  try {\n    \/\/ \u8bfb\u53d6\u6587\u4ef6\n    BufferedReader reader = new BufferedReader(new FileReader(path));\n    String line;\n    while ((line = reader.readLine()) != null) {\n      \/\/ \u89e3\u6790\u884c\n    }\n  } catch (IOException e) {\n    \/\/ \u6587\u4ef6\u8bfb\u53d6\u6216\u89e3\u6790\u9519\u8bef\n  } finally {\n    \/\/ \u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0c\u90fd\u5173\u95ed\u6587\u4ef6\n    reader.close();\n  }\n}<\/pre>\n<p><strong>\u4f18\u52bf<\/strong><\/p>\n<p>\u4f7f\u7528 try-catch-finally \u5757\u7ba1\u7406\u5f02\u5e38\u5177\u6709\u4ee5\u4e0b\u4f18\u52bf\uff1a<\/p>\n<ul>\n<li><strong>\u6e05\u6670\u7684\u5f02\u5e38\u5904\u7406\uff1a<\/strong>\u5b83\u660e\u786e\u6307\u5b9a\u4e86\u5728\u53d1\u751f\u4e0d\u540c\u5f02\u5e38\u65f6\u8981\u6267\u884c\u7684\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u4fdd\u8bc1\u8d44\u6e90\u91ca\u653e\uff1a<\/strong>finally \u5757\u786e\u4fdd\u5728\u6240\u6709\u60c5\u51b5\u4e0b\u90fd\u4f1a\u91ca\u653e\u8d44\u6e90\uff0c\u9632\u6b62\u8d44\u6e90\u6cc4\u9732\u3002<\/li>\n<li><strong>\u53ef\u7ef4\u62a4\u6027\uff1a<\/strong>\u5c06\u5f02\u5e38\u5904\u7406\u4e0e\u6b63\u5e38\u6d41\u7a0b\u5206\u79bb\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u6613\u4e8e\u7ef4\u62a4\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u4f7f\u7528 try-catch-finally \u5757\uff0c\u60a8\u53ef\u4ee5\u7f16\u5199\u5065\u58ee\u4e14\u53ef\u9760\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u80fd\u591f\u4f18\u96c5\u5730\u5904\u7406\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u5982\u4f55\u4f7f\u7528 try-catch-finally \u5757\u6709\u6548\u5730\u7ba1\u7406\u5f02\u5e38\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8ejava,\u5f02\u5e38\u5904\u7406\u7684\u8d44\u6599\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-202850","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202850","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=202850"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202850\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}