{"id":732,"date":"2023-03-23T21:50:23","date_gmt":"2023-03-23T13:50:23","guid":{"rendered":""},"modified":"2023-03-23T21:50:23","modified_gmt":"2023-03-23T13:50:23","slug":"Java \u5f02\u5e38\u5904\u7406","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/732.html","title":{"rendered":"Java \u5f02\u5e38\u5904\u7406"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Java \u5f02\u5e38\u5904\u7406<\/h1>\n<\/p><\/div>\n
\n Java\u4e2d\u7684
\n \u5f02\u5e38\u5904\u7406<\/strong>\u662f\u5904\u7406\u8fd0\u884c\u65f6\u9519\u8bef\u7684\u5f3a\u5927
\n \u673a\u5236\u4e4b\u4e00<\/em>\uff0c\u56e0\u6b64\u53ef\u4ee5\u4fdd\u6301\u5e94\u7528\u7a0b\u5e8f\u7684\u6b63\u5e38\u8fd0\u884c\u3002\n <\/div>\n
\n \u5728\u6b64\u9875\u9762\u4e2d\uff0c\u6211\u4eec\u5c06\u5b66\u4e60Java\u5f02\u5e38\uff0c\u5176\u7c7b\u578b\u4ee5\u53ca\u5df2\u68c0\u67e5\u548c\u672a\u68c0\u67e5\u7684\u5f02\u5e38\u4e4b\u95f4\u7684\u533a\u522b\u3002\n <\/div>\n

Java\u4e2d\u7684\u5f02\u5e38\u662f\u4ec0\u4e48<\/h2>\n
\n \u5b57\u5178\u542b\u4e49: <\/strong>\u5f02\u5e38\u662f\u4e00\u79cd\u5f02\u5e38\u60c5\u51b5\u3002\n <\/div>\n
\n \u5728Java\u4e2d\uff0c\u5f02\u5e38\u662f\u4e00\u79cd\u7834\u574f\u7a0b\u5e8f\u6b63\u5e38\u6d41\u7a0b\u7684\u4e8b\u4ef6\u3002\u5b83\u662f\u5728\u8fd0\u884c\u65f6\u629b\u51fa\u7684\u5bf9\u8c61\u3002\n <\/div>\n
\n

\u4ec0\u4e48\u662f\u5f02\u5e38\u5904\u7406<\/h2>\n
\n \u5f02\u5e38\u5904\u7406\u662f\u4e00\u79cd\u7528\u4e8e\u5904\u7406\u8fd0\u884c\u65f6\u9519\u8bef\u7684\u673a\u5236\uff0c\u4f8b\u5982ClassNotFoundException\uff0cIOException\uff0cSQLException\uff0cRemoteException\u7b49\u3002\n <\/div>\n

\u5f02\u5e38\u5904\u7406\u7684\u4f18\u52bf<\/h3>\n
\n \u5f02\u5e38\u5904\u7406\u7684\u6838\u5fc3\u4f18\u52bf\u662f
\n \u4fdd\u6301\u5e94\u7528\u7a0b\u5e8f\u7684\u6b63\u5e38\u8fd0\u884c<\/strong> \u3002\u5f02\u5e38\u901a\u5e38\u4f1a\u4e2d\u65ad\u5e94\u7528\u7a0b\u5e8f\u7684\u6b63\u5e38\u6d41\u7a0b\uff0c\u8fd9\u5c31\u662f\u6211\u4eec\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u7684\u539f\u56e0\u3002\u8ba9\u6211\u4eec\u6765\u4e00\u4e2a\u573a\u666f:\n <\/div>\n
\n
 statement 1;
statement 2;
statement 3;
statement 4;
statement 5;
\/\/exception occursstatement 6;
statement 7;
statement 8;
statement 9;
statement 10;
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5047\u8bbe\u60a8\u7684\u7a0b\u5e8f\u4e2d\u670910\u6761\u8bed\u53e5\uff0c\u5e76\u4e14\u5728\u8bed\u53e55\u5904\u53d1\u751f\u5f02\u5e38\uff0c\u5176\u4f59\u4ee3\u7801\u5c06\u4e0d\u6267\u884c\uff0c\u5373\u8bed\u53e56\u81f310\u5c06\u4e0d\u6267\u884c\u3002\u5982\u679c\u6211\u4eec\u6267\u884c\u5f02\u5e38\u5904\u7406\uff0c\u5219\u8bed\u53e5\u7684\u5176\u4f59\u90e8\u5206\u5c06\u88ab\u6267\u884c\u3002\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u6211\u4eec\u5728Java\u4e2d\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u7684\u539f\u56e0\u3002\n <\/div>\n

<\/p>\n

\n\u60a8\u77e5\u9053\u5417\uff1f <\/legend>\n

\u5df2\u68c0\u67e5\u548c\u672a\u68c0\u67e5\u7684\u5f02\u5e38\u4e4b\u95f4\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/span>
\n \u4ee3\u7801int data = 50\/0;\u540e\u9762\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f<\/span>
\n \u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528\u591a\u4e2a\u6355\u83b7\u5757\uff1f<\/span>
\n \u4e0d\u6267\u884cfinally\u5757\u662f\u5426\u6709\u53ef\u80fd\uff1f<\/span>
\n \u4ec0\u4e48\u662f\u5f02\u5e38\u4f20\u64ad\uff1f<\/span>
\n throw\u548cthrows\u5173\u952e\u5b57\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/span>
\n \u5c06\u5f02\u5e38\u5904\u7406\u4e0e\u65b9\u6cd5\u8986\u76d6\u4e00\u8d77\u4f7f\u7528\u76844\u6761\u89c4\u5219\u662f\u4ec0\u4e48\uff1f<\/span>
\n <\/fieldset>\n

Java Exception\u7c7b\u7684\u5c42\u6b21\u7ed3\u6784<\/h2>\n
\n java.lang.Throwable\u7c7b\u662fJava Exception\u7684\u6839\u7c7b\u7531\u4e24\u4e2a\u5b50\u7c7b\u7ee7\u627f\u7684\u5c42\u6b21\u7ed3\u6784: \u5f02\u5e38\u548c\u9519\u8bef\u3002 Java Exception\u7c7b\u7684\u5c42\u6b21\u7ed3\u6784\u5982\u4e0b:\n <\/div>\n

Java \u5f02\u5e38\u5904\u7406_https:\/\/bianchenghao6.com_\u3010Java \u57fa\u7840\u6559\u7a0b\u3011_\u7b2c1\u5f20 <\/p>\n

Java\u5f02\u5e38\u7c7b\u578b<\/h3>\n
\n \u4e3b\u8981\u6709\u4e24\u79cd\u7c7b\u578b\u7684\u5f02\u5e38: \u9009\u4e2d\u548c\u672a\u9009\u4e2d\u3002\u5728\u8fd9\u91cc\uff0c\u9519\u8bef\u88ab\u89c6\u4e3a\u672a\u68c0\u67e5\u7684\u5f02\u5e38\u3002\u6839\u636eOracle\u7684\u4ecb\u7ecd\uff0c\u5b58\u5728\u4e09\u79cd\u7c7b\u578b\u7684\u5f02\u5e38:\n <\/div>\n

\u5df2\u68c0\u67e5\u7684\u5f02\u5e38<\/span>
\n \u672a\u7ecf\u68c0\u67e5\u7684\u5f02\u5e38<\/span>
\n \u9519\u8bef<\/span>
\n Java \u5f02\u5e38\u5904\u7406_https:\/\/bianchenghao6.com_\u3010Java \u57fa\u7840\u6559\u7a0b\u3011_\u7b2c2\u5f20 <\/p>\n

\u5df2\u68c0\u67e5\u548c\u672a\u68c0\u67e5\u7684\u5f02\u5e38\u4e4b\u95f4\u7684\u533a\u522b<\/h2>\n

1)\u5df2\u68c0\u67e5\u7684\u5f02\u5e38<\/h3>\n
\n \u76f4\u63a5\u7ee7\u627fThrowable\u7c7b(\u9664RuntimeException\u548cError\u5916)\u7684\u7c7b\u79f0\u4e3a\u5df2\u68c0\u67e5\u7684\u5f02\u5e38\uff0c\u4f8b\u5982IOException\uff0cSQLException\u7b49\u3002\u5728\u7f16\u8bd1\u65f6\u68c0\u67e5\u5df2\u68c0\u67e5\u7684\u5f02\u5e38\u3002\n <\/div>\n

2)\u672a\u68c0\u67e5\u7684\u5f02\u5e38<\/h3>\n
\n \u7ee7\u627fRuntimeException\u7684\u7c7b\u79f0\u4e3a\u672a\u7ecf\u68c0\u67e5\u7684\u5f02\u5e38\uff0c\u4f8b\u5982ArithmeticException\uff0cNullPointerException\uff0cArrayIndexOutOfBoundsException\u7b49\u3002\u672a\u7ecf\u68c0\u67e5\u7684\u5f02\u5e38\u4e0d\u5728\u7f16\u8bd1\u65f6\u68c0\u67e5\uff0c\u4f46\u5728\u8fd0\u884c\u65f6\u68c0\u67e5\u3002\n <\/div>\n

3)\u9519\u8bef<\/h3>\n
\n \u9519\u8bef\u662f\u65e0\u6cd5\u6062\u590d\u7684\uff0c\u4f8b\u5982OutOfMemoryError\uff0cVirtualMachineError\uff0cAssertionError\u7b49\u3002\n <\/div>\n
\n

Java\u5f02\u5e38\u5173\u952e\u5b57<\/h2>\n
\n \u67095\u4e2a\u5173\u952e\u5b57\u7528\u4e8e\u5904\u7406Java\u4e2d\u7684\u5f02\u5e38\u3002\n <\/div>\n\n\n\n\n\n\n\n\n
\u5173\u952e\u5b57<\/td>\n\u8bf4\u660e<\/td>\n<\/tr>\n
try<\/td>\n\"try\"\u5173\u952e\u5b57\u7528\u4e8e\u6307\u5b9a\u4e00\u4e2a\u653e\u7f6e\u5f02\u5e38\u4ee3\u7801\u7684\u5757\u3002\u5728try\u5757\u4e4b\u540e\u5fc5\u987b\u662fcatch\u6216finally\u3002\u8fd9\u610f\u5473\u7740\uff0c\u6211\u4eec\u4e0d\u80fd\u5355\u72ec\u4f7f\u7528try\u5757\u3002<\/td>\n<\/tr>\n
catch<\/td>\n\"catch\"\u5757\u7528\u4e8e\u5904\u7406\u5f02\u5e38\u3002\u5b83\u5fc5\u987b\u5728try\u5757\u4e4b\u524d\uff0c\u8fd9\u610f\u5473\u7740\u6211\u4eec\u4e0d\u80fd\u5355\u72ec\u4f7f\u7528catch\u5757\u3002\u4e4b\u540e\u53ef\u4ee5\u7a0d\u540e\u518d\u8fdb\u884c\u963b\u6b62\u3002<\/td>\n<\/tr>\n
finally<\/td>\n\"finally\"\u5757\u7528\u4e8e\u6267\u884c\u7a0b\u5e8f\u7684\u91cd\u8981\u4ee3\u7801\u3002\u65e0\u8bba\u662f\u5426\u5904\u7406\u5f02\u5e38\u90fd\u5c06\u6267\u884c\u3002<\/td>\n<\/tr>\n
throw\/td> <\/td>\n\"throw\"\u5173\u952e\u5b57\u7528\u4e8e\u5f15\u53d1\u5f02\u5e38\u3002<\/td>\n<\/tr>\n
throws<\/td>\n\"throws\"\u5173\u952e\u5b57\u7528\u4e8e\u58f0\u660e\u5f02\u5e38\u3002\u5b83\u4e0d\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002\u5b83\u6307\u5b9a\u65b9\u6cd5\u4e2d\u53ef\u80fd\u53d1\u751f\u5f02\u5e38\u3002\u5b83\u603b\u662f\u4e0e\u65b9\u6cd5\u7b7e\u540d\u4e00\u8d77\u4f7f\u7528\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

Java\u5f02\u5e38\u5904\u7406\u793a\u4f8b<\/h2>\n
\n \u6211\u4eec\u6765\u770b\u4e00\u4e2aJava\u5f02\u5e38\u5904\u7406\u793a\u4f8b\uff0c\u5728\u8be5\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528try-catch\u8bed\u53e5\u6765\u5904\u7406\u5f02\u5e38\u3002\n <\/div>\n
\n
 public <\/span>class <\/span>JavaExceptionExample{
    public <\/span>static void <\/span>main(String args[]){
        try{
            \/\/code that may raise exception int <\/span>data=100\/0;
        }
        catch(ArithmeticException e){
            System.out.println<\/span>(e);
        }
        \/\/rest <\/span>code of the program System.out.println<\/span>(\"rest <\/span>of the code...\"<\/span>);
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa:\n <\/div>\n
\n
 Exception in thread main java.lang.ArithmeticException:\/ by zerorest <\/span>of the code...
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c100\/0 \u5f15\u53d1ArithmeticException\uff0c\u8be5\u5f02\u5e38\u7531try-catch\u5757\u5904\u7406\u3002\n <\/div>\n
\n

Java\u5f02\u5e38\u7684\u5e38\u89c1\u60c5\u51b5<\/h2>\n
\n \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u4f1a\u53d1\u751f\u672a\u7ecf\u68c0\u67e5\u7684\u5f02\u5e38\u3002\u5b83\u4eec\u5982\u4e0b:\n <\/div>\n

1)\u53d1\u751fArithmeticException\u7684\u60c5\u51b5<\/h3>\n
\n \u5982\u679c\u5c06\u4efb\u4f55\u6570\u5b57\u9664\u4ee5\u96f6\uff0c\u5c31\u4f1a\u53d1\u751fArithmeticException\u3002\n <\/div>\n
\n
 int <\/span>a=50\/0;
\/\/ArithmeticException
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n

2)\u53d1\u751fNullPointerException\u7684\u60c5\u51b5<\/h3>\n
\n \u5982\u679c\u6211\u4eec\u5728\u4efb\u4f55\u53d8\u91cf\u4e2d\u90fd\u6709\u7a7a\u503c\uff0c\u5219\u6267\u884c\u5bf9\u53d8\u91cf\u7684\u4efb\u4f55\u64cd\u4f5c\u90fd\u5c06\u5f15\u53d1NullPointerException\u3002\n <\/div>\n
\n
 String s=null;
System.out.println<\/span>(s.length<\/span>());
\/\/NullPointerException
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n

3)\u53d1\u751fNumberFormatException\u7684\u60c5\u51b5<\/h3>\n
\n \u4efb\u4f55\u503c\u7684\u683c\u5f0f\u9519\u8bef\u90fd\u53ef\u80fd\u53d1\u751fNumberFormatException\u3002\u5047\u8bbe\u6211\u6709\u4e00\u4e2a\u5305\u542b\u5b57\u7b26\u7684string\u53d8\u91cf\uff0c\u5c06\u8be5\u53d8\u91cf\u8f6c\u6362\u4e3a\u6570\u5b57\u5c06\u53d1\u751fNumberFormatException\u3002\n <\/div>\n
\n
 String s=\"abc\"<\/span>;
int <\/span>i=Integer.parseInt<\/span>(s);
\/\/NumberFormatException
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n

4)\u53d1\u751fArrayIndexOutOfBoundsException\u7684\u60c5\u51b5<\/h3>\n
\n \u5982\u679c\u5728\u9519\u8bef\u7684\u7d22\u5f15\u4e2d\u63d2\u5165\u4efb\u4f55\u503c\uff0c\u5c06\u5bfc\u81f4ArrayIndexOutOfBoundsException\uff0c\u5982\u4e0b\u6240\u793a:\n <\/div>\n
\n
 int <\/span>a[]=new int[5];
a[10]=50;
\/\/ArrayIndexOutOfBoundsException
<\/span><\/code><\/pre>\n<\/p><\/div>\n

Java\u5f02\u5e38\u7d22\u5f15<\/h2>\n

Java try-catch <\/span>
\n Java\u591a\u91cd\u6355\u83b7\u5757 <\/span>
\n Java try\u5d4c\u5957 <\/span>
\n Java finally\u5173\u952e\u5b57 <\/span>
\n Java Throw\u5173\u952e\u5b57 <\/span>
\n Java\u5f02\u5e38\u4f20\u64ad <\/span>
\n Java Throws\u5173\u952e\u5b57 <\/span>
\n Java Throw vs Throws <\/span>
\n Java Final vs Final vs Finalize <\/span>
\n \u5177\u6709\u65b9\u6cd5\u8986\u76d6\u7684Java\u5f02\u5e38\u5904\u7406 <\/span>
\n Java\u81ea\u5b9a\u4e49\u5f02\u5e38 <\/span>
\n <\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Java \u5f02\u5e38\u5904\u7406zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111],"tags":[],"class_list":["post-732","post","type-post","status-publish","format-standard","hentry","category-java-jichu"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/732"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=732"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/732\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=732"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}