{"id":941,"date":"2022-10-15T18:02:29","date_gmt":"2022-10-15T10:02:29","guid":{"rendered":"https:\/\/qaqaq.top\/?p=941"},"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%a4%84%e7%90%86%e5%bc%82%e5%b8%b8%ef%bc%9athrows%e6%96%b9%e5%bc%8f%e3%80%81%e5%bc%80%e5%8f%91%e4%b8%ad%e5%a6%82%e4%bd%95%e9%80%89%e6%8b%a9%e5%93%aa%e7%a7%8d","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=941","title":{"rendered":"\u5f02\u5e38\u5904\u7406-\u5904\u7406\u5f02\u5e38\uff1athrows\u65b9\u5f0f\u3001\u5f00\u53d1\u4e2d\u5982\u4f55\u9009\u62e9\u54ea\u79cd\u65b9\u5f0f\u5904\u7406\u5f02\u5e38"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P378;\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\n\/*\r\n * \u5f02\u5e38\u5904\u7406\u7684\u65b9\u5f0f\u4e8c\uff1athrows + \u5f02\u5e38\u7c7b\u578b\r\n * \r\n * 1. \"throws + \u5f02\u5e38\u7c7b\u578b\"\u5199\u5728\u65b9\u6cd5\u7684\u58f0\u660e\u5904\u3002\u6307\u660e\u6b64\u65b9\u6cd5\u6267\u884c\u65f6\uff0c\u53ef\u80fd\u4f1a\u629b\u51fa\u7684\u5f02\u5e38\u7c7b\u578b\u3002\r\n * \t\t\u4e00\u65e6\u5f53\u65b9\u6cd5\u4f53\u6267\u884c\u65f6\uff0c\u51fa\u73b0\u5f02\u5e38\uff0c\u4ecd\u4f1a\u5728\u5f02\u5e38\u4ee3\u7801\u5904\u751f\u6210\u4e00\u4e2a\u5f02\u5e38\u7c7b\u7684\u5bf9\u8c61\uff0c\u6b64\u5bf9\u8c61\u6ee1\u8db3throws\u540e\u5f02\u5e38\r\n * \t\t\u7c7b\u578b\u65f6\uff0c\u5c31\u4f1a\u88ab\u629b\u51fa\u3002\u5f02\u5e38\u4ee3\u7801\u540e\u7eed\u7684\u4ee3\u7801\uff0c\u5c31\u4e0d\u518d\u6267\u884c\uff01\r\n * \r\n * 2. \u4f53\u4f1a\uff1atry-catch-finally\uff1a\u771f\u6b63\u7684\u5c06\u5f02\u5e38\u7ed9\u5904\u7406\u6389\u4e86\u3002\r\n * \t\t\tthrows\u7684\u65b9\u5f0f\u53ea\u662f\u5c06\u5f02\u5e38\u629b\u7ed9\u4e86\u65b9\u6cd5\u7684\u8c03\u7528\u8005\u3002\u5e76\u6ca1\u6709\u771f\u6b63\u5c06\u5f02\u5e38\u5904\u7406\u6389\u3002\r\n * \r\n * 3. \u5f00\u53d1\u4e2d\u5982\u4f55\u9009\u62e9\u4f7f\u7528 try-catch-finlally \u8fd8\u662f\u4f7f\u7528 throws \uff1f\r\n * \t\t3.1 \u5982\u679c\u7236\u7c7b\u4e2d\u88ab\u91cd\u5199\u7684\u65b9\u6cd5\u6ca1\u6709throws\u65b9\u5f0f\u5904\u7406\u5f02\u5e38\uff0c\u5219\u5b50\u7c7b\u91cd\u5199\u7684\u65b9\u6cd5\u4e5f\u4e0d\u80fd\u4f7f\u7528throws\uff0c\u610f\u5473\u7740\u5982\u679c\r\n * \t\t\t\u5b50\u7c7b\u91cd\u5199\u7684\u65b9\u6cd5\u4e2d\u6709\u5f02\u5e38\uff0c\u5fc5\u987b\u4f7f\u7528try-catch-finally\u65b9\u5f0f\u5904\u7406\u3002\r\n * \t\t3.2 \u6267\u884c\u7684\u65b9\u6cd5\u4e2d\uff0c\u5148\u540e\u53c8\u8c03\u7528\u4e86\u53e6\u5916\u7684\u51e0\u4e2a\u65b9\u6cd5\uff0c\u8fd9\u51e0\u4e2a\u65b9\u6cd5\u662f\u9012\u8fdb\u5173\u7cfb\u6267\u884c\u7684\u3002\u6211\u4eec\u5efa\u8bae\u8fd9\u51e0\u4e2a\u65b9\u6cd5\u4f7f\u7528throws\r\n * \t\t\t\u7684\u65b9\u5f0f\u8fdb\u884c\u5904\u7406\u3002\u800c\u6267\u884c\u7684\u65b9\u6cd5a\u53ef\u4ee5\u8003\u8651\u4f7f\u7528try-catch-finally\u65b9\u5f0f\u8fdb\u884c\u5904\u7406\u3002\r\n * \r\n * \r\n * \r\n *\/\r\npublic class ExceptionTest2 {\r\n\t\r\n\tpublic static void main(String&#91;] args){\r\n\t\ttry {\r\n\t\t\tmethod2();\t\r\n\t\t\t\r\n\t\t}catch(IOException e) {\r\n\t\t\te.printStackTrace();\r\n\t\t}\r\n\t\t\r\n\/\/\t\tmethod3();\r\n\t\t\r\n\t}\r\n\t\r\n\tpublic static void method3() {\r\n\t\ttry {\r\n\t\t\tmethod2();\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\tpublic static void method2() throws IOException{\r\n\t\tmethod1();\r\n\t}\r\n\r\n\tpublic static void method1() throws FileNotFoundException, IOException{\r\n\t\tFile file = new File(\"hello1.txt\");\r\n\t\tFileInputStream fis = new FileInputStream(file);\r\n\t\t\r\n\t\tint data = fis.read();\r\n\t\twhile(data != -1) {\r\n\t\t\tSystem.out.print((char)data);\r\n\t\t\tdata = fis.read();\r\n\t\t}\r\n\t\t\r\n\t\tfis.close();\r\n\t\t\r\n\t\tSystem.out.println(\"hahaha!\");\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-941","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\/941"}],"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=941"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/941\/revisions"}],"predecessor-version":[{"id":942,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/941\/revisions\/942"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=941"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}