{"id":1345,"date":"2022-02-27T15:45:46","date_gmt":"2022-02-27T07:45:46","guid":{"rendered":"http:\/\/www.zyhcoding.club\/?p=1345"},"modified":"2022-02-27T20:36:47","modified_gmt":"2022-02-27T12:36:47","slug":"java-%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"http:\/\/www.zyhcoding.club\/index.php\/2022\/02\/27\/java-%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86\/","title":{"rendered":"JAVA \u5f02\u5e38\u5904\u7406"},"content":{"rendered":"<h5>\u5f02\u5e38Exception<\/h5>\n<p>\u5f02\u5e38\u5c31\u662f\u5728\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u53d1\u751f\u7684\u3001\u7834\u574f\u7a0b\u5e8f\u6b63\u5e38\u6307\u4ee4\u6d41\u7684\u4e8b\u4ef6\u3002\u5f53\u65b9\u6cd5\u4e2d\u51fa\u73b0\u9519\u8bef\u65f6\uff0c\u8be5\u65b9\u6cd5\u5c31\u4f1a\u521b\u5efa\u51fa\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5e76\u628a\u5b83\u4f20\u7ed9\u8fd0\u884c\u65f6\u7cfb\u7edf\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u6210\u4e3a<code>\u5f02\u5e38\u5bf9\u8c61<\/code>\uff0c\u5b83\u542b\u6709\u9519\u8bef\u7684\u76f8\u5173\u4fe1\u606f\uff0c\u5305\u62ec\u7c7b\u578b\u548c\u9519\u8bef\u53d1\u751f\u65f6\u7a0b\u5e8f\u7684\u72b6\u6001\u3002\u521b\u5efa\u5f02\u5e38\u5bf9\u8c61\uff0c\u5e76\u628a\u5176\u4f20\u7ed9\u8fd0\u884c\u65f6\u7cfb\u7edf\uff0c\u6210\u4e3a<code>\u629b\u51fa\u5f02\u5e38<\/code>\u3002<\/p>\n<p>\u5f53\u4e00\u4e2a\u65b9\u6cd5\u629b\u51fa\u5f02\u5e38\u540e\uff0c\u8fd0\u884c\u65f6\u7cfb\u7edf\u4f1a\u5c1d\u8bd5\u5904\u7406\u8be5\u5f02\u5e38\uff0c\u53ef\u80fd\u4f1a\u62a5\u9519\u7684\u65b9\u6cd5\u6240\u8c03\u7528\u7684\u65b9\u6cd5\u7684\u6709\u5e8f\u5217\u8868\uff0c\u6210\u4e3a<code>\u8c03\u7528\u6808<\/code>\u3002\u5c31\u662f\u65b9\u6cd5\u7684\u8c03\u7528\u5c42\u6b21\u3002<\/p>\n<p>\u8fd0\u884c\u65f6\u7cfb\u7edf\u6309\u7167\u65b9\u6cd5\u8c03\u7528\u7684\u9006\u5e8f\u641c\u7d22\u8c03\u7528\u6808\uff0c\u627e\u5230\u5305\u542b\u5904\u7406\u5f02\u5e38\u7684\u4ee3\u7801\u5757\u7684\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u4ee3\u7801\u5757\u6210\u4e3a\u5f02\u5e38\u5904\u7406\u51fd\u6570\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u5c31\u662f<code>\u6355\u83b7\u5f02\u5e38<\/code>\uff0c\u5982\u679c\u6ca1\u6709\u627e\u5230\u9002\u7528\u7684\u5f02\u5e38\u5904\u7406\u51fd\u6570\uff0c\u8fd0\u884c\u65f6\u7cfb\u7edf\u7ec8\u6b62\u3002<\/p>\n<p>\u5f02\u5e38\u662f\u7a0b\u5e8f\u4e2d\u7684\u4e00\u4e9b\u9519\u8bef\u3002\u5f02\u5e38\u53d1\u751f\u7684\u539f\u56e0\u6709\u5f88\u591a\uff0c\u901a\u5e38\u5305\u542b\u4ee5\u4e0b\u51e0\u5927\u7c7b\uff1a<\/p>\n<ul>\n<li>\u7528\u6237\u8f93\u5165\u4e86\u975e\u6cd5\u6570\u636e\u3002<\/li>\n<li>\u8981\u6253\u5f00\u7684\u6587\u4ef6\u4e0d\u5b58\u5728\u3002<\/li>\n<li>\u7f51\u7edc\u901a\u4fe1\u65f6\u8fde\u63a5\u4e2d\u65ad\uff0c\u6216\u8005JVM\u5185\u5b58\u6ea2\u51fa\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u4e9b\u5f02\u5e38\u6709\u7684\u662f\u56e0\u4e3a\u7528\u6237\u9519\u8bef\u5f15\u8d77\uff0c\u6709\u7684\u662f\u7a0b\u5e8f\u9519\u8bef\u5f15\u8d77\u7684\uff0c\u8fd8\u6709\u5176\u5b83\u4e00\u4e9b\u662f\u56e0\u4e3a\u7269\u7406\u9519\u8bef\u5f15\u8d77\u7684\u3002<\/p>\n<p>\u5f02\u5e38\u8fd8\u53ef\u4ee5\u5206\u4e3a\u68c0\u67e5\u6027\u5f02\u5e38\u3001\u8fd0\u884c\u65f6\u5f02\u5e38\u3001\u9519\u8bef\u3002<\/p>\n<h5>Exception\u7c7b<\/h5>\n<p>\u6240\u6709\u7684\u5f02\u5e38\u7c7b\u90fd\u662f\u4ecejava.lang.Exception\u7c7b\u7ee7\u627f\u7684\u5b50\u7c7b\u3002Exception\u7c7b\u662fThrowable\u7c7b\u7684\u5b50\u7c7b\u3002\u9664\u4e86Exception\u7c7b\u5916\uff0cThrowable\u7c7b\u8fd8\u6709\u4e00\u4e2a\u5b50\u7c7bError\u3002<br \/>\nJava \u7a0b\u5e8f\u901a\u5e38\u4e0d\u6355\u83b7\u9519\u8bef\u3002\u9519\u8bef\u4e00\u822c\u53d1\u751f\u5728\u4e25\u91cd\u6545\u969c\u65f6\uff0c\u5b83\u4eec\u5728Java\u7a0b\u5e8f\u5904\u7406\u7684\u8303\u7574\u4e4b\u5916\u3002Error \u7528\u6765\u6307\u793a\u8fd0\u884c\u65f6\u73af\u5883\u53d1\u751f\u7684\u9519\u8bef\u3002<br \/>\n\u5f02\u5e38\u7c7b\u6709\u4e24\u4e2a\u4e3b\u8981\u7684\u5b50\u7c7b\uff1aIOException \u7c7b\u548c RuntimeException \u7c7b\u3002<br \/>\n<img src=\"http:\/\/www.zyhcoding.club:80\/wp-content\/uploads\/2022\/02\/\u5f02\u5e38\u7c7b.png\" alt=\"\" \/><\/p>\n<p>\u5728 Java \u5185\u7f6e\u7c7b\u4e2d\uff0c\u6709\u5927\u90e8\u5206\u5e38\u7528\u68c0\u67e5\u6027\u548c\u975e\u68c0\u67e5\u6027\u5f02\u5e38\u3002<\/p>\n<p>Java\u5185\u7f6e\u5f02\u5e38\u7c7b\u5728Java\u8bed\u8a00\u7684java.lang\u6807\u51c6\u5305\u4e2d\uff0c\u7531\u4e8e\u8be5\u5305\u662f\u9ed8\u8ba4\u52a0\u8f7d\u5230\u6240\u6709\u7684Java\u7a0b\u5e8f\u4e2d\u7684\uff0c\u6240\u4ee5\u5927\u90e8\u5206\u4ece\u8fd0\u884c\u65f6\u5f02\u5e38\u7c7b\u7ee7\u627f\u800c\u6765\u7684\u5f02\u5e38\u90fd\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u3002<\/p>\n<p>\u4e0b\u9762\u7684\u8868\u4e2d\u5217\u51fa\u4e86 Java \u7684\u975e\u68c0\u67e5\u6027\u5f02\u5e38\u3002<br \/>\n<img src=\"http:\/\/www.zyhcoding.club:80\/wp-content\/uploads\/2022\/02\/Java\u975e\u68c0\u67e5\u5f02\u5e38.png\" alt=\"\" \/><\/p>\n<p>\u4e0b\u9762\u7684\u8868\u4e2d\u5217\u51fa\u4e86 Java \u5b9a\u4e49\u5728 java.lang \u5305\u4e2d\u7684\u68c0\u67e5\u6027\u5f02\u5e38\u7c7b\u3002<img src=\"http:\/\/www.zyhcoding.club:80\/wp-content\/uploads\/2022\/02\/Java\u68c0\u67e5\u5f02\u5e38.png\" alt=\"\" \/><\/p>\n<p>\u5f02\u5e38\u7c7b\u91cc\u9762\u4e5f\u6709\u4e00\u4e9b\u65b9\u6cd5\uff1a<br \/>\n<img src=\"http:\/\/www.zyhcoding.club:80\/wp-content\/uploads\/2022\/02\/\u5f02\u5e38\u65b9\u6cd5.png\" alt=\"\" \/><\/p>\n<h5>\u6355\u83b7\u5f02\u5e38<\/h5>\n<pre><code class=\"language-java\">try{\n    \/\/ \u7a0b\u5e8f\u5757\n} catch (Exception e1) {\n    \/\/ catch \u5757\n} catch (Exception e2) {\n    \/\/ catch \u5757\n} finally {\n    \/\/ finally \u5757\n}<\/code><\/pre>\n<p>\u4f7f\u7528 try \u548c catch \u5173\u952e\u5b57\u53ef\u4ee5\u6355\u83b7\u5f02\u5e38\u3002try\/catch \u4ee3\u7801\u5757\u653e\u5728\u5f02\u5e38\u53ef\u80fd\u53d1\u751f\u7684\u5730\u65b9\u3002try\/catch\u4ee3\u7801\u5757\u4e2d\u7684\u4ee3\u7801\u79f0\u4e3a\u4fdd\u62a4\u4ee3\u7801\u3002<br \/>\nfinally \u5173\u952e\u5b57\u7528\u6765\u521b\u5efa\u5728 try \u4ee3\u7801\u5757\u540e\u9762\u6267\u884c\u7684\u4ee3\u7801\u5757\u3002\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0cfinally \u4ee3\u7801\u5757\u4e2d\u7684\u4ee3\u7801\u603b\u4f1a\u88ab\u6267\u884c\u3002\u5728 finally \u4ee3\u7801\u5757\u4e2d\uff0c\u53ef\u4ee5\u8fd0\u884c\u6e05\u7406\u7c7b\u578b\u7b49\u6536\u5c3e\u5584\u540e\u6027\u8d28\u7684\u8bed\u53e5\u3002<\/p>\n<pre><code class=\"language-java\">import java.io.*;\npublic class ExcepTest{\n\n   public static void main(String args[]){\n      try{\n         int a[] = new int[2];\n         System.out.println(&quot;Access element three :&quot; + a[3]);\n      }catch(ArrayIndexOutOfBoundsException e){\n         System.out.println(&quot;Exception thrown  :&quot; + e);\n      }\n   }\n}\n\ntry {\n    file = new FileInputStream(fileName);\n    x = (byte) file.read();\n} catch(FileNotFoundException f) { \/\/ Not valid!\n    f.printStackTrace();\n    return -1;\n} catch(IOException i) {\n    i.printStackTrace();\n    return -1;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5f02\u5e38Exception \u5f02\u5e38\u5c31\u662f\u5728\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u53d1\u751f\u7684\u3001\u7834\u574f\u7a0b\u5e8f\u6b63\u5e38\u6307\u4ee4\u6d41\u7684\u4e8b\u4ef6\u3002\u5f53\u65b9\u6cd5\u4e2d\u51fa\u73b0\u9519\u8bef\u65f6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[75],"tags":[],"_links":{"self":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/1345"}],"collection":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/comments?post=1345"}],"version-history":[{"count":4,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/1345\/revisions"}],"predecessor-version":[{"id":1353,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/1345\/revisions\/1353"}],"wp:attachment":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/media?parent=1345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/categories?post=1345"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/tags?post=1345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}