{"id":289,"date":"2015-04-18T15:32:05","date_gmt":"2015-04-18T07:32:05","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=289"},"modified":"2021-04-12T10:55:25","modified_gmt":"2021-04-12T02:55:25","slug":"java%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e6%a6%82%e8%bf%b0","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=289","title":{"rendered":"Java\u5f02\u5e38\u5904\u7406\u6982\u8ff0"},"content":{"rendered":"<p>\u4e00\u0081\u7a0b\u5e8f\u0006\u7684\u9519\u8bef\u53ef\u5206\u4e09\u7c7b\uff1a\u7f16\u8bd1\u9519\u8bef\u3001\u903b\u8f91\u9519\u8bef\u548c\u8fd0\u884c\u65f6\u9519\u8bef\u3002<br \/>\n1)\u7f16\u8bd1\u9519\u8bef\u662f\u7531\u4e8e\u6ca1\u6709\u9075\u5faaJava\u8bed\u8a00\u7684\u8bed\u6cd5\u89c4\u5219\u800c\u4ea7\u751f\u7684\uff0c\u8fd9\u79cd\u9519\u8bef\u8981\u5728\u7f16\u8bd1\u9636\u6bb5\u6392\u9664\uff0c\u5426\u5219\u7a0b\u5e8f\u4e0d\u53ef\u80fd\u8fd0\u884c\u3002<\/p>\n<p>2)\u903b\u8f91\u9519\u8bef\u662f\u6307\u7a0b\u5e8f\u7f16\u8bd1\u6b63\u5e38\uff0c\u4e5f\u80fd\u8fd0\u884c\uff0c\u4f46\u7ed3\u679c\u4e0d\u662f\u4eba\u4eec\u6240\u671f\u5f85\u7684\u3002<\/p>\n<p>3)\u8fd0\u884c\u65f6\u9519\u8bef\u662f\u6307\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u4e86\u4e00\u4e2a\u4e0d\u53ef\u80fd\u6267\u884c\u7684\u64cd\u4f5c\uff0c\u5c31\u4f1a\u51fa\u73b0\u8fd0\u884c\u65f6\u9519\u8bef\uff0c\u8fd0\u884c\u65f6\u9519\u8bef\u6709\u65f6\u4e5f\u53ef\u4ee5\u7531\u903b\u8f91\u9519\u8bef\u5f15\u8d77\u3002<br \/>\n<!--more--><br \/>\n\u5f02\u5e38\u5904\u7406\u7684\u4e3b\u8981\u76ee\u7684\u662f\u5373\u4f7f\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u53d1\u751f\u4e86\u9519\u8bef\uff0c\u4e5f\u8981\u4fdd\u8bc1\u7a0b\u5e8f\u80fd\u6b63\u5e38\u7ed3\u675f\uff0c\u907f\u514d\u7531\u4e8e\u9519\u8bef\u800c\u4f7f\u6b63\u5728\u8fd0\u884c\u7684\u7a0b\u5e8f\u4e2d\u9014\u505c\u6b62\u3002<br \/>\n\u5f02\u5e38\u5904\u7406\u673a\u5236\u662fJAVA\u8bed\u8a00\u7684\u91cd\u8981\u7279\u5f81\u4e4b\u4e00\u3002\u901a\u8fc7\u5f02\u5e38\u5904\u7406\u673a\u5236\u53ef\u9632\u6b62\u7a0b\u5e8f\u6267\u884c\u671f\u95f4\u56e0\u51fa\u73b0\u9519\u8bef\u800c\u9020\u6210\u4e0d\u53ef\u9884\u6599\u7684\u7ed3\u679c\u3002<\/p>\n<p>\u5f02\u5e38\u662f\u7a0b\u5e8f\u6267\u884c\u671f\u95f4\u53d1\u751f\u7684\u5404\u79cd\u610f\u5916\u6216\u9519\u8bef\u3002\u6bd4\u5982\u8bf4\uff1a<br \/>\n\u2460 \u7528\u6237\u8f93\u5165\u51fa\u9519<br \/>\n\u2461 \u6240\u9700\u6587\u4ef6\u627e\u4e0d\u5230<br \/>\n\u2462 \u8fd0\u884c\u65f6\u78c1\u76d8\u7a7a\u95f4\u4e0d\u591f<br \/>\n\u2463 \u5185\u5b58\u4e0d\u591f<br \/>\n\u2464 \u7b97\u672f\u8fd0\u7b97\u9519 (\u6570\u7684\u6ea2\u51fa\uff0c\u88ab\u96f6\u9664\u2026)<br \/>\n\u2465 \u6570\u7ec4\u4e0b\u6807\u8d8a\u754c<br \/>\n\u4e8c\u3001\u5728Java\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u0006\uff0c\u4ea7\u751f\u7684\u5f02\u5e38\u901a\u5e38\u6709\u4e09\u79cd\u7c7b\u578b\uff1a<\/p>\n<p>1.\u662fJAVA\u865a\u62df\u673a\u7531\u4e8e\u67d0\u4e9b\u5185\u90e8\u9519\u8befError\u4ea7\u751f\u7684\u5f02\u5e38\uff0c\u8fd9\u7c7b\u5f02\u5e38\u4e0d\u5728\u7528\u6237\u7a0b\u5e8f\u7684\u63a7\u5236\u4e4b\u5185\uff0c\u4e5f\u4e0d\u9700\u8981\u7528\u6237\u5904\u7406\u8fd9\u7c7b\u5f02\u5e38\uff1b<\/p>\n<p>2.\u662f\u6807\u51c6\u5f02\u5e38\u7c7b\uff0c\u7531java\u7cfb\u7edf\u9884\u5148\u5b9a\u4e49\u597d\u7684\u3002\u8fd9\u7c7b\u5f02\u5e38\u662f\u7531\u7a0b\u5e8f\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\u800c\u4ea7\u751f\u7684\uff0c\u5982\uff1a\u4ee5\u96f6\u4e3a\u9664\u6570\u7684\u9664\u6cd5\uff0c\u8bbf\u95ee\u6570\u7ec4\u4e0b\u6807\u8303\u56f4\u4ee5\u5916\u7684\u6570\u7ec4\u5143\u7d20\uff0c\u8bbf\u95ee\u7a7a\u5bf9\u8c61\u5185\u7684\u4fe1\u606f\uff0c\u8fd9\u662f\u9700\u8981\u7528\u6237\u7a0b\u5e8f\u5904\u7406\u7684\u5f02\u5e38\u3002<\/p>\n<p>3.\u662f\u6839\u636e\u9700\u8981\u5728\u7528\u6237\u7a0b\u5e8f\u4e2d\u81ea\u5b9a\u4e49\u7684\u4e00\u4e9b\u5f02\u5e38\u7c7b\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u4e0b\u9762\u4ee3\u7801\u8fd0\u884c\u65f6\u4f1a\u51fa\u73b0\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38.<\/p>\n<p>[java]\/\/java\u76f4\u63a5\u5904\u7406\u8fd0\u884c\u65f6\u5f02\u5e38<br \/>\nclass TestSystemException{<br \/>\n   public static void main(String[ ] args){<br \/>\n      int\u3000num[ ]=new int[2];<br \/>\n      for(int i=1;i&lt;3;i++){<br \/>\n            num[i]=i;<br \/>\n            System.out.println(\u201cnum[\u201d+i+\u201d]=\u201d+i);<br \/>\n     }<br \/>\n }<br \/>\n}[\/java]<\/p>\n<p>\u4e09\u3001\u5f02\u5e38\u7684\u5904\u7406\u673a\u5236<\/p>\n<p>Java\u5bf9\u5f02\u5e38\u7684\u5904\u7406\u6d89\u53ca\u4e24\u65b9\u9762\u7684\u5185\u5bb9\uff1a<br \/>\n\u4e00\u662f\u629b\u51fa\uff08throw\uff09\u5f02\u5e38<br \/>\n\u4e8c\u662f\u6355\u6349\uff08catch\uff09\u5f02\u5e38<br \/>\n\u5982\u679c\u7a0b\u5e8f\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u4e86\u8fd0\u884c\u9519\u8bef\uff0c\u5e76\u4e14\u4ea7\u751f\u7684\u5f02\u5e38\u4e0e\u7cfb\u7edf\u4e2d\u9884\u5b9a\u4e49\u67d0\u4e2a\u5f02\u5e38\u7c7b\u76f8\u5bf9\u5e94\uff0c\u7cfb\u7edf\u5c31\u81ea\u52a8\u4ea7\u751f\u4e00\u4e2a\u8be5\u5f02\u5e38\u7c7b\u7684\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a\u629b\u51fa\uff08throw\uff09\u5f02\u5e38\u3002\u5f53\u6709\u5f02\u5e38\u5bf9\u8c61\u629b\u51fa\u65f6\uff0c\u5c06\u5728\u7a0b\u5e8f\u4e2d\u5bfb\u627e\u5904\u7406\u8fd9\u4e2a\u5f02\u5e38\u7684\u4ee3\u7801\uff0c\u5982\u679c\u627e\u5230\u5904\u7406\u4ee3\u7801\uff0c\u5219\u628a\u5f02\u5e38\u5bf9\u8c61\u4ea4\u7ed9\u8be5\u6bb5\u4ee3\u7801\u8fdb\u884c\u5904\u7406\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a\u6355\u6349\uff08catch\uff09\u5f02\u5e38\u3002\u5982\u679c\u7a0b\u5e8f\u4e2d\u6ca1\u6709\u7ed9\u51fa\u5904\u7406\u5f02\u5e38\u7684\u4ee3\u7801\uff0c\u5219\u628a\u5f02\u5e38\u4ea4\u7ed9Java\u8fd0\u884c\u7cfb\u7edf\u9ed8\u8ba4\u7684\u5f02\u5e38\u5904\u7406\u4ee3\u7801\u8fdb\u884c\u5904\u7406\u3002\u9ed8\u8ba4\u7684\u5904\u7406\u65b9\u5f0f\u662f\uff1a\u9996\u5148\u663e\u793a\u63cf\u8ff0\u5f02\u5e38\u4fe1\u606f\u7684\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u7ec8\u6b62\u7a0b\u5e8f\u7684\u8fd0\u884c\u3002<br \/>\n\u56db\u3001\u5f02\u5e38\u7684\u629b\u51fa\uff08throw\uff09<\/p>\n<p>\u629b\u51fa\u5f02\u5e38\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a\u7531\u7cfb\u7edf\u81ea\u52a8\u629b\u51fa\u548c\u5229\u7528\u629b\u51fa\u8bed\u53e5\u629b\u51fa\u3002<br \/>\n1 \u7531\u7cfb\u7edf\u81ea\u52a8\u629b\u51fa\u5f02\u5e38<br \/>\n\u5728\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\uff0c\u5982\u679c\u51fa\u73b0\u4e86\u53ef\u88abJava\u8fd0\u884c\u7cfb\u7edf\u8bc6\u522b\u7684\u9519\u8bef\uff0c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u4ea7\u751f\u4e0e\u8be5\u9519\u8bef\u76f8\u5bf9\u5e94\u7684\u5f02\u5e38\u7c7b\u7684\u5bf9\u8c61\uff0c\u5373\u81ea\u52a8\u629b\u51fa\u3002<br \/>\n2 \u4eba\u4e3a\u5f02\u5e38\u629b\u51fa<br \/>\n\u4e24\u79cd\u65b9\u5f0f\uff1a<br \/>\n\u2460 \u5728\u65b9\u6cd5\u5934\u5199\u51fa\u9700\u8981\u629b\u51fa\u7684\u5f02\u5e38\uff08\u5229\u7528throws\u8bed\u53e5\uff09<\/p>\n<p>[java]class Throws_Exp{<br \/>\n   public static void main(String[] args) throws ArithmeticException,ArrayIndexOutOfBoundsException<br \/>\n      {<br \/>\n        int a=4,b=0,c[]={1,2,3,4,5};<br \/>\n          System.out.println(a\/b);<br \/>\n          System.out.println(c[a+1]);<br \/>\n          System.out.println(\u201cend\u201d);<br \/>\n  }<br \/>\n}[\/java]<\/p>\n<p>\u2461 \u5728\u65b9\u6cd5\u4f53\u5185\u629b\u51fa\u5f02\u5e38\uff08\u5229\u7528throw\u8bed\u53e5\uff09<\/p>\n<p>[java]class Throw_Exp3 {<br \/>\n  public static void main(String[] args) {<br \/>\n    int a=5,b=0,c[]={1,2,3,4,5};<br \/>\n    System.out.println(\u201cBefore  throw\u201d);<br \/>\n     if(b==0)<br \/>\n          throw (new  ArithmeticException ());<br \/>\n    System.out.println(a\/b);<br \/>\n     if(a&gt;4)<br \/>\n          throw (new ArrayIndexOutOfBoundsException());<br \/>\n     System.out.println(a\/b);<br \/>\n  }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>\u4e94\u3001\u6355\u6349\u5f02\u5e38<\/p>\n<p>try \u2013 catch\u8bed\u53e5\u5757\u683c\u5f0f\uff1a<\/p>\n<p>[java]try<br \/>\n {<br \/>\n   \/\/\u5728\u6b64\u533a\u57df\u5185\u6216\u80fd\u53d1\u751f\u5f02\u5e38;<br \/>\n }catch(\u5f02\u5e38\u7c7b1   e1)<br \/>\n  { \/\/\u5904\u7406\u5f02\u5e381; }<br \/>\n   \u2026<br \/>\n  catch(\u5f02\u5e38\u7c7bn   en)<br \/>\n { \/\/\u5904\u7406\u5f02\u5e38n; }<br \/>\n [finally<br \/>\n   { \/\/\u4e0d\u8bba\u5f02\u5e38\u662f\u5426\u53d1\u751f,\u90fd\u8981\u6267\u884c\u7684\u90e8\u5206; }<br \/>\n ][\/java]<\/p>\n<p>\u516d\u3001\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u5f02\u5e38\u7c7b<\/p>\n<p>\u5c3d\u7ba1Java\u63d0\u4f9b\u4e86\u5f88\u591a\u5f02\u5e38\u7c7b\uff0c\u4f46\u7528\u6237\u8fd8\u662f\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5b9a\u4e49\u81ea\u5df1\u7684\u5f02\u5e38\u7c7b\uff0c\u5373\u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u3002<br \/>\n\u8bf4\u660e\uff1a<br \/>\n\uff081\uff09\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u5f02\u5e38\u7c7b\u5fc5\u987b\u662fThrowable\u7c7b\u6216Exception\u7c7b\u7684\u5b50\u7c7b\u3002<br \/>\n\uff082\uff09\u81ea\u5b9a\u4e49\u7684\u5f02\u5e38\u7c7b\uff0c\u4e00\u822c\u53ea\u8981\u58f0\u660e\u4e24\u4e2a\u6784\u9020\u65b9\u6cd5\uff0c\u4e00\u4e2a\u662f\u4e0d\u7528\u53c2\u6570\u7684\uff0c\u53e6\u4e00\u4e2a\u4ee5\u5b57\u7b26\u4e32\u4e3a\u53c2\u6570\u3002\u4f5c\u4e3a\u6784\u9020\u65b9\u6cd5\u53c2\u6570\u7684\u5b57\u7b26\u4e32\u5e94\u5f53\u53cd\u6620\u5f02\u5e38\u7684\u4fe1\u606f\u3002<br \/>\n(3)\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u683c\u5f0f<br \/>\nclass MyException extends Exception{<br \/>\n\u2026<br \/>\n}<br \/>\n\u6ce8:\u7528\u6237\u5b9a\u4e49\u7684\u5f02\u5e38\u540c\u6837\u8981\u7528try&#8211;catch\u6355\u83b7,\u4f46\u5fc5\u987b\u7531\u7528\u6237\u81ea\u5df1\u629b\u51fa throw new MyException().<\/p>\n<p>[java]class Exception_exp{<br \/>\n         public static void main(String[] args){<br \/>\n             try {<br \/>\n              System.out.println(&quot;2+3=&quot;+add(2,3));<br \/>\n                    System.out.println(&quot;-8+10=&quot;+add(-8,10));<br \/>\n             }catch (Exception e){<br \/>\n             System.out.println(&quot;Exception is &quot;+e);<br \/>\n         }<br \/>\n       }<br \/>\n       public static int add(int n,int m) throws UserException {<br \/>\n               if (n&lt;0|| m&lt;0) throw new UserException();<br \/>\n              return n+m;<br \/>\n       }<br \/>\n}<br \/>\nclass UserException extends Exception{<br \/>\n         UserException(){<br \/>\n          super(&quot;\u6570\u636e\u4e3a\u8d1f\u6570&quot;);<br \/>\n       }<br \/>\n }[\/java]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u0081\u7a0b\u5e8f\u0006\u7684\u9519\u8bef\u53ef\u5206\u4e09\u7c7b\uff1a\u7f16\u8bd1\u9519\u8bef\u3001\u903b\u8f91\u9519\u8bef\u548c\u8fd0\u884c\u65f6\u9519\u8bef\u3002 1)\u7f16\u8bd1\u9519\u8bef\u662f\u7531\u4e8e\u6ca1\u6709\u9075\u5faaJava\u8bed\u8a00\u7684\u8bed\u6cd5\u89c4\u5219\u800c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[21],"tags":[],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/289"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=289"}],"version-history":[{"count":12,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/289\/revisions"}],"predecessor-version":[{"id":3238,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/289\/revisions\/3238"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}