{"id":937,"date":"2022-10-15T18:01:36","date_gmt":"2022-10-15T10:01:36","guid":{"rendered":"https:\/\/qaqaq.top\/?p=937"},"modified":"2022-11-27T12:39:52","modified_gmt":"2022-11-27T04:39:52","slug":"%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86-%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e6%96%b9%e5%bc%8f%e6%a6%82%e8%bf%b0%e3%80%81%e5%a4%84%e7%90%86%e5%bc%82%e5%b8%b8%ef%bc%9atry-catch%e6%96%b9%e5%bc%8f%e3%80%81","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=937","title":{"rendered":"\u5f02\u5e38\u5904\u7406-\u5f02\u5e38\u5904\u7406\u65b9\u5f0f\u6982\u8ff0\u3001\u5904\u7406\u5f02\u5e38\uff1atry-catch\u65b9\u5f0f\u3001\u7f16\u8bd1\u65f6\u5f02\u5e38\u548c\u8fd0\u884c\u65f6\u5f02\u5e38\u7684\u4e0d\u540c\u5904\u7406\u3001\u624b\u52a8\u629b\u51fa\u5f02\u5e38\u5bf9\u8c61"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P374;\r\n\r\nimport java.io.File;\r\nimport java.io.FileInputStream;\r\nimport java.io.FileNotFoundException;\r\nimport java.io.IOException;\r\n\r\nimport org.junit.Test;\r\n\r\n\/*\r\n * \u4e00\u3001\u5f02\u5e38\u7684\u5904\u7406\uff1a\u6293\u629b\u6a21\u578b\r\n * \r\n * \u8fc7\u7a0b\u4e00\uff1a\"\u629b\"\uff1a\u7a0b\u5e8f\u5728\u6b63\u5e38\u6267\u884c\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4e00\u65e6\u51fa\u73b0\u5f02\u5e38\uff0c\u5c31\u4f1a\u5728\u5f02\u5e38\u4ee3\u7801\u5904\u751f\u6210\u4e00\u4e2a\u5bf9\u5e94\u5f02\u5e38\u7c7b\u7684\u5bf9\u8c61\u3002\r\n * \t\t\t\t\u5e76\u5c06\u6b64\u5bf9\u8c61\u629b\u51fa\u3002\r\n * \t\t\t\t\u4e00\u65e6\u629b\u51fa\u5bf9\u8c61\u4ee5\u540e\uff0c\u5176\u540e\u7684\u4ee3\u7801\u5c31\u4e0d\u518d\u6267\u884c\u3002\r\n * \r\n * \t\t\t\u5173\u4e8e\u5f02\u5e38\u5bf9\u8c61\u7684\u4ea7\u751f\uff1a\u2460 \u7cfb\u7edf\u81ea\u52a8\u751f\u6210\u7684\u5f02\u5e38\u5bf9\u8c61\r\n * \t\t\t\t\t\t\t\u2461 \u624b\u52a8\u7684\u751f\u6210\u4e00\u4e2a\u5f02\u5e38\u5bf9\u8c61\uff0c\u5e76\u629b\u51fa\uff08throw\uff09\r\n * \r\n * \u8fc7\u7a0b\u4e8c\uff1a\"\u6293\"\uff1a\u53ef\u4ee5\u7406\u89e3\u4e3a\u5f02\u5e38\u7684\u5904\u7406\u65b9\u5f0f\uff1a\u2460 try-catch-finally \u2461 throws\r\n * \r\n * \r\n * \u4e8c\u3001try-catch-finally\u7684\u4f7f\u7528\r\n * \r\n * try{\r\n * \t\t\/\/\u53ef\u80fd\u51fa\u73b0\u5f02\u5e38\u7684\u4ee3\u7801\r\n * \r\n * }catch(\u5f02\u5e38\u7c7b\u578b1 \u53d8\u91cf\u540d1){\r\n * \t\t\/\/\u5904\u7406\u5f02\u5e38\u7684\u65b9\u5f0f1\r\n * }catch(\u5f02\u5e38\u7c7b\u578b2 \u53d8\u91cf\u540d2){\r\n * \t\t\/\/\u5904\u7406\u5f02\u5e38\u7684\u65b9\u5f0f2\r\n * }catch(\u5f02\u5e38\u7c7b\u578b3 \u53d8\u91cf\u540d3){\r\n * \t\t\/\/\u5904\u7406\u5f02\u5e38\u7684\u65b9\u5f0f3\r\n * }\r\n * ....\r\n * finally{\r\n * \t\t\/\/\u4e00\u5b9a\u4f1a\u6267\u884c\u7684\u4ee3\u7801\r\n * }\r\n * \r\n * \u8bf4\u660e\uff1a\r\n * 1. finally\u662f\u53ef\u9009\u7684\u3002\r\n * 2. \u4f7f\u7528try\u5c06\u53ef\u80fd\u51fa\u73b0\u5f02\u5e38\u4ee3\u7801\u5305\u88c5\u8d77\u6765\uff0c\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\uff0c\u4e00\u65e6\u51fa\u73b0\u5f02\u5e38\uff0c\u5c31\u4f1a\u751f\u6210\u4e00\u4e2a\u5bf9\u5e94\u5f02\u5e38\u7c7b\u7684\u5bf9\u8c61\uff0c\u6839\u636e\u6b64\u5bf9\u8c61\r\n * \t\t\u7684\u7c7b\u578b\uff0c\u53bbcatch\u4e2d\u8fdb\u884c\u5339\u914d\r\n * 3. \u4e00\u65e6try\u4e2d\u7684\u5f02\u5e38\u5bf9\u8c61\u5339\u914d\u5230\u67d0\u4e00\u4e2acatch\u65f6\uff0c\u5c31\u8fdb\u5165catch\u4e2d\u8fdb\u884c\u5f02\u5e38\u7684\u5904\u7406\u3002\u4e00\u65e6\u5904\u7406\u5b8c\u6210\uff0c\u5c31\u8df3\u51fa\u5f53\u524d\u7684\r\n * \t\ttry-catch\u7ed3\u6784\uff08\u5728\u6ca1\u6709\u5199finally\u7684\u60c5\u51b5\uff09\u3002\u7ee7\u7eed\u6267\u884c\u5176\u540e\u7684\u4ee3\u7801\r\n * 4. catch\u4e2d\u7684\u5f02\u5e38\u7c7b\u578b\u5982\u679c\u6ca1\u6709\u5b50\u7236\u7c7b\u5173\u7cfb\uff0c\u5219\u8c01\u58f0\u660e\u5728\u4e0a\uff0c\u8c01\u58f0\u660e\u5728\u4e0b\u65e0\u6240\u8c13\u3002\r\n * \t\tcatch\u4e2d\u7684\u5f02\u5e38\u7c7b\u578b\u5982\u679c\u6ee1\u8db3\u5b50\u7236\u7c7b\u5173\u7cfb\uff0c\u5219\u8981\u6c42\u5b50\u7c7b\u4e00\u5b9a\u58f0\u660e\u5728\u7236\u7c7b\u7684\u4e0a\u9762\u3002\u5426\u5219\uff0c\u62a5\u9519\r\n * 5. \u5e38\u7528\u7684\u5f02\u5e38\u5bf9\u8c61\u5904\u7406\u7684\u65b9\u5f0f\uff1a\u2460 String getMessage()\t\t\u2461 printStackTrace()\r\n * 6. \u5728try\u7ed3\u6784\u4e2d\u58f0\u660e\u7684\u53d8\u91cf\uff0c\u518d\u51fa\u4e86try\u7ed3\u6784\u4ee5\u540e\uff0c\u5c31\u4e0d\u80fd\u518d\u88ab\u8c03\u7528\r\n * 7. try-catch-finally\u7ed3\u6784\u53ef\u4ee5\u5d4c\u5957\r\n * \r\n * \u4f53\u4f1a1\uff1a\u4f7f\u7528try-catch-finally\u5904\u7406\u7f16\u8bd1\u65f6\u5f02\u5e38\uff0c\u4f7f\u5f97\u7a0b\u5e8f\u5728\u7f16\u8bd1\u65f6\u5c31\u4e0d\u518d\u62a5\u9519\uff0c\u4f46\u662f\u8fd0\u884c\u65f6\u4ecd\u53ef\u80fd\u62a5\u9519\u3002\r\n * \t\t\u76f8\u5f53\u4e8e\u6211\u4eec\u4f7f\u7528try-catch-finally\u5c06\u4e00\u4e2a\u7f16\u8bd1\u65f6\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\uff0c\u5ef6\u8fdf\u5230\u8fd0\u884c\u65f6\u51fa\u73b0\u3002\r\n * \u4f53\u4f1a2\uff1a\u5f00\u53d1\u4e2d\uff1a\u7531\u4e8e\u8fd0\u884c\u65f6\u5f02\u5e38\u6bd4\u8f83\u5e38\u89c1\uff1a\u6240\u4ee5\u6211\u4eec\u901a\u5e38\u5c31\u4e0d\u9488\u5bf9\u8fd0\u884c\u65f6\u5f02\u5e38\u7f16\u5199try-catch-finally\u4e86\u3002\r\n * \t\t\u9488\u5bf9\u7f16\u8bd1\u65f6\u5f02\u5e38\uff0c\u6211\u4eec\u8bf4\u4e00\u5b9a\u8981\u8003\u8651\u5f02\u5e38\u7684\u5904\u7406\u3002\r\n *\/\r\npublic class ExceptionTest1 {\r\n\r\n\t@Test\r\n\tpublic void test2() {\r\n\t\ttry {\r\n\t\t\tFile file = new File(\"hello.txt\");\r\n\t\t\tFileInputStream fis = new FileInputStream(file);\r\n\t\t\t\r\n\t\t\tint data = fis.read();\r\n\t\t\twhile(data != -1) {\r\n\t\t\t\tSystem.out.print((char)data);\r\n\t\t\t\tdata = fis.read();\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tfis.close();\r\n\t\t}catch(FileNotFoundException e) {\r\n\t\t\te.printStackTrace();\r\n\t\t}catch(IOException e) {\r\n\t\t\te.printStackTrace();\r\n\t\t}\r\n\t}\r\n\t\r\n\t@Test\r\n\tpublic void Test1() {\r\n\t\t\r\n\t\tString str = \"123\";\r\n\t\tstr = \"abc\";\r\n\t\tint num = 0;\r\n\t\ttry {\r\n\t\t\tnum = Integer.parseInt(str);\r\n\t\t\t\r\n\t\t\tSystem.out.println(\"helo-----1\");\r\n\t\t}catch(NumberFormatException e) {\r\n\/\/\t\t\tSystem.out.println(\"\u51fa\u73b0\u6570\u503c\u8f6c\u6362\u5f02\u5e38\u4e86\uff0c\u4e0d\u8981\u7740\u6025....\");\r\n\t\t\t\/\/String getMessage():\r\n\/\/\t\t\tSystem.out.println(e.getMessage());\r\n\t\t\t\/\/printStackTrace():\r\n\t\t\te.printStackTrace();\r\n\t\t}catch(NullPointerException e) {\r\n\t\t\tSystem.out.println(\"\u51fa\u73b0\u7a7a\u6307\u9488\u5f02\u5e38\u4e86\uff0c\u4e0d\u8981\u7740\u6025....\");\r\n\t\t}catch(Exception e) {\r\n\t\t\tSystem.out.println(\"\u51fa\u73b0\u5f02\u5e38\u4e86\uff0c\u4e0d\u8981\u7740\u6025....\");\r\n\t\t}\r\n\t\tSystem.out.println(num);\r\n\t\t\r\n\t\t\r\n\t\tSystem.out.println(\"hello-----2\");\r\n\t\t\r\n\t\t\r\n\t\t\r\n\t\t\r\n\t}\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[46],"class_list":["post-937","post","type-post","status-publish","format-standard","hentry","category-java-code","tag-java"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/937"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=937"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/937\/revisions"}],"predecessor-version":[{"id":938,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/937\/revisions\/938"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=937"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=937"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=937"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}