{"id":3986,"date":"2018-06-13T21:06:56","date_gmt":"2018-06-13T13:06:56","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=3986"},"modified":"2018-06-13T21:06:56","modified_gmt":"2018-06-13T13:06:56","slug":"java%e4%b8%ad%e7%9a%84%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/3986.html","title":{"rendered":"Java\u4e2d\u7684\u5f02\u5e38\u5904\u7406"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u5728\u5b66\u4e60Java\u7684\u8fc7\u7a0b\u4e2d\u4e0d\u65ad\u7528\u6587\u7ae0\u8fdb\u884c\u6574\u7406\u603b\u7ed3\uff08\u5e38\u7528\u529f\u80fd\u7684Java\u5b9e\u73b0\uff09\uff0c\u4e89\u53d6\u65e9\u65e5\u80fd\u8f83\u4e3a\u719f\u7ec3\u7684\u4f7f\u7528Java\u8fdb\u884c\u5f00\u53d1\u3002<\/p>\n<h4 id=\"id-\u6a21\u677f-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<p><span style=\"color: #ff0000;\">\u5f02\u5e38\u662f\u7a0b\u5e8f\u4e2d\u7684\u4e00\u4e9b\u9519\u8bef\uff0c\u4f46\u5e76\u4e0d\u662f\u6240\u6709\u7684\u9519\u8bef\u90fd\u662f\u5f02\u5e38\uff0c\u5e76\u4e14\u9519\u8bef\u6709\u65f6\u5019\u662f\u53ef\u4ee5\u907f\u514d\u7684\u3002<\/span><\/p>\n<p>\u8981\u7406\u89e3Java\u5f02\u5e38\u5904\u7406\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u4f60\u9700\u8981\u638c\u63e1\u4ee5\u4e0b\u4e09\u79cd\u7c7b\u578b\u7684\u5f02\u5e38\uff1a<\/p>\n<ul>\n<li><strong>\u68c0\u67e5\u6027\u5f02\u5e38\uff1a<\/strong>\u6700\u5177\u4ee3\u8868\u7684\u68c0\u67e5\u6027\u5f02\u5e38\u662f\u7528\u6237\u9519\u8bef\u6216\u95ee\u9898\u5f15\u8d77\u7684\u5f02\u5e38\uff0c\u8fd9\u662f\u7a0b\u5e8f\u5458\u65e0\u6cd5\u9884\u89c1\u7684\u3002\u4f8b\u5982\u8981\u6253\u5f00\u4e00\u4e2a\u4e0d\u5b58\u5728\u6587\u4ef6\u65f6\uff0c\u4e00\u4e2a\u5f02\u5e38\u5c31\u53d1\u751f\u4e86\uff0c\u8fd9\u4e9b\u5f02\u5e38\u5728\u7f16\u8bd1\u65f6\u4e0d\u80fd\u88ab\u7b80\u5355\u5730\u5ffd\u7565\u3002<\/li>\n<li><strong>\u8fd0\u884c\u65f6\u5f02\u5e38\uff08RuntimeException\uff09\uff1a<\/strong>\u00a0\u8fd0\u884c\u65f6\u5f02\u5e38\u662f\u53ef\u80fd\u88ab\u7a0b\u5e8f\u5458\u907f\u514d\u7684\u5f02\u5e38\u3002\u4e0e\u68c0\u67e5\u6027\u5f02\u5e38\u76f8\u53cd\uff0c\u8fd0\u884c\u65f6\u5f02\u5e38\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u88ab\u5ffd\u7565\u3002<\/li>\n<li><strong>\u9519\u8bef\uff08Error\uff09\uff1a<\/strong>\u00a0\u9519\u8bef\u4e0d\u662f\u5f02\u5e38\uff0c\u800c\u662f\u8131\u79bb\u7a0b\u5e8f\u5458\u63a7\u5236\u7684\u95ee\u9898\u3002\u9519\u8bef\u5728\u4ee3\u7801\u4e2d\u901a\u5e38\u88ab\u5ffd\u7565\u3002\u4f8b\u5982\uff0c\u5f53\u6808\u6ea2\u51fa\u65f6\uff0c\u4e00\u4e2a\u9519\u8bef\u5c31\u53d1\u751f\u4e86\uff0c\u5b83\u4eec\u5728\u7f16\u8bd1\u4e5f\u68c0\u67e5\u4e0d\u5230\u7684\u3002<\/li>\n<\/ul>\n<h5><strong>Exception \u7c7b\u7684\u5c42\u6b21<\/strong><\/h5>\n<p><strong><span style=\"color: #ff0000;\">\u6240\u6709\u7684\u5f02\u5e38\u7c7b\u662f\u4ece java.lang.Exception \u7c7b\u7ee7\u627f\u7684\u5b50\u7c7b\u3002<\/span><\/strong><\/p>\n<p><strong>Exception \u7c7b\u662f Throwable \u7c7b\u7684\u5b50\u7c7b\u3002\u9664\u4e86Exception\u7c7b\u5916\uff0cThrowable\u8fd8\u6709\u4e00\u4e2a\u5b50\u7c7bError \u3002<\/strong><\/p>\n<p><span style=\"color: #ff0000;\"><strong>Java \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\u3002<\/strong><\/span>Error \u7528\u6765\u6307\u793a\u8fd0\u884c\u65f6\u73af\u5883\u53d1\u751f\u7684\u9519\u8bef\u3002\u4f8b\u5982\uff0cJVM \u5185\u5b58\u6ea2\u51fa\u3002\u4e00\u822c\u5730\uff0c\u7a0b\u5e8f\u4e0d\u4f1a\u4ece\u9519\u8bef\u4e2d\u6062\u590d\u3002<\/p>\n<p>\u5f02\u5e38\u7c7b\u6709\u4e24\u4e2a\u4e3b\u8981\u7684\u5b50\u7c7b\uff1aIOException \u7c7b\u548c RuntimeException \u7c7b\u3002<\/p>\n<h5><strong>\u6355\u83b7\u5f02\u5e38<\/strong><\/h5>\n<p>\u4f7f\u7528 try \u548c catch \u5173\u952e\u5b57\u53ef\u4ee5\u6355\u83b7\u5f02\u5e38\u3002<span style=\"color: #ff0000;\"><strong>try\/catch \u4ee3\u7801\u5757\u653e\u5728\u5f02\u5e38\u53ef\u80fd\u53d1\u751f\u7684\u5730\u65b9\u3002<\/strong><\/span>try\/catch\u4ee3\u7801\u5757\u4e2d\u7684\u4ee3\u7801\u79f0\u4e3a\u4fdd\u62a4\u4ee3\u7801\uff0c\u4f7f\u7528 try\/catch \u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:default decode:true \">try {\r\n   \/\/ \u7a0b\u5e8f\u4ee3\u7801\r\n} catch(ExceptionName e1) {\r\n   \/\/Catch \u5757\r\n}<\/pre>\n<h5><strong>\u591a\u91cd\u6355\u83b7\u5757<\/strong><\/h5>\n<p>\u4e00\u4e2a try \u4ee3\u7801\u5757\u540e\u9762\u8ddf\u968f\u591a\u4e2a catch \u4ee3\u7801\u5757\u7684\u60c5\u51b5\u5c31\u53eb\u591a\u91cd\u6355\u83b7\u3002\u591a\u91cd\u6355\u83b7\u5757\u7684\u8bed\u6cd5\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<div class=\"example\">\n<div class=\"example_code\">\n<pre class=\"lang:default decode:true \">try{\r\n   \/\/ \u7a0b\u5e8f\u4ee3\u7801\r\n} catch(\u5f02\u5e38\u7c7b\u578b1 \u5f02\u5e38\u7684\u53d8\u91cf\u540d1) {\r\n  \/\/ \u7a0b\u5e8f\u4ee3\u7801\r\n} catch(\u5f02\u5e38\u7c7b\u578b2 \u5f02\u5e38\u7684\u53d8\u91cf\u540d2) {\r\n  \/\/ \u7a0b\u5e8f\u4ee3\u7801\r\n} catch(\u5f02\u5e38\u7c7b\u578b2 \u5f02\u5e38\u7684\u53d8\u91cf\u540d2) {\r\n  \/\/ \u7a0b\u5e8f\u4ee3\u7801\r\n}<\/pre>\n<h5><strong>finally\u5173\u952e\u5b57<\/strong><\/h5>\n<p><span style=\"color: #ff0000;\">finally \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<\/span>finally \u4ee3\u7801\u5757\u51fa\u73b0\u5728 catch \u4ee3\u7801\u5757\u6700\u540e\uff0c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<div class=\"example\">\n<div class=\"example_code\">\n<pre class=\"lang:default decode:true\">try{\r\n  \/\/ \u7a0b\u5e8f\u4ee3\u7801\r\n} catch(\u5f02\u5e38\u7c7b\u578b1 \u5f02\u5e38\u7684\u53d8\u91cf\u540d1) {\r\n  \/\/ \u7a0b\u5e8f\u4ee3\u7801\r\n} catch(\u5f02\u5e38\u7c7b\u578b2 \u5f02\u5e38\u7684\u53d8\u91cf\u540d2) {\r\n  \/\/ \u7a0b\u5e8f\u4ee3\u7801\r\n} finally {\r\n  \/\/ \u7a0b\u5e8f\u4ee3\u7801\r\n}<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p><strong><span style=\"color: #ff0000;\">\u5728\u5b9e\u9645\u9879\u76ee\u5f00\u53d1\u4e2d\uff0c\u5efa\u8bae\u5c06\u53ef\u80fd\u4ea7\u751f\u5f02\u5e38\u7684\u4ee3\u7801\u5757\u90fd\u653e\u5728try..catch\u4e2d\uff0c\u5e76\u5bf9\u53ef\u80fd\u5f71\u54cd\u7a0b\u5e8f\u6838\u5fc3\u529f\u80fd\u7684\u5f02\u5e38\u8fdb\u884c\u5b9e\u65f6\u901a\u77e5\uff0c\u907f\u514d\u51fa\u73b0\u7a0b\u5e8f\u5f02\u5e38\u800c\u4e0d\u81ea\u77e5\u7684\u60c5\u51b5\u3002<\/span><\/strong><\/p>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"http:\/\/www.runoob.com\/java\/java-exceptions.html\">Java\u5f02\u5e38\u5904\u7406<\/a><\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/qq_35246620\/article\/details\/80660429\">\u6548\u7387\u7f16\u7a0b \u4e4b\u300c\u5f02\u5e38\u300d<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u5728\u5b66\u4e60Java\u7684\u8fc7\u7a0b\u4e2d\u4e0d\u65ad\u7528\u6587\u7ae0\u8fdb\u884c\u6574\u7406\u603b\u7ed3\uff08\u5e38\u7528\u529f\u80fd\u7684Java\u5b9e\u73b0\uff09\uff0c\u4e89\u53d6\u65e9\u65e5\u80fd\u8f83\u4e3a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[518,61,1159],"class_list":["post-3986","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-exception","tag-java","tag-1159"],"views":3322,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3986","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=3986"}],"version-history":[{"count":2,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3986\/revisions"}],"predecessor-version":[{"id":3992,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3986\/revisions\/3992"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=3986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=3986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=3986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}