{"id":289,"date":"2015-04-18T15:32:05","date_gmt":"2015-04-18T07:32:05","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=289"},"modified":"2026-08-18T12:09:30","modified_gmt":"2026-08-18T04:09:30","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":"<section id=\"wenyan\" style=\"font-family: system-ui, 'Apple Color Emoji', 'Segoe UI', 'Segoe UI Symbol', 'Noto Sans', Roboto, sans-serif; line-height: 1.75; font-size: 16px; color: #000000; caret-color: #000000;\" data-provider=\"WenYan\">\n<p style=\"margin: 1em 0px;\">\u7a0b\u5e8f\u91cc\u7684\u9519\u8bef\u53ef\u5206\u4e09\u7c7b\uff1a<strong style=\"color: #4870ac;\">\u7f16\u8bd1\u9519\u8bef<\/strong>\u3001<strong style=\"color: #4870ac;\">\u903b\u8f91\u9519\u8bef<\/strong>\u548c<strong style=\"color: #4870ac;\">\u8fd0\u884c\u65f6\u9519\u8bef<\/strong>\u3002\u5f02\u5e38\u5904\u7406\u7684\u76ee\u6807\u662f\uff1a\u5373\u4fbf\u8fd0\u884c\u4e2d\u51fa\u9519\uff0c\u4e5f\u5c3d\u91cf\u8ba9\u7a0b\u5e8f\u6b63\u5e38\u7ed3\u675f\uff0c\u800c\u4e0d\u662f\u4e2d\u9014\u505c\u6b7b\u3002<\/p>\n<p><!--more--><\/p>\n<table style=\"display: table; text-align: left; overflow: auto; border-collapse: collapse; margin: 1.4em auto; max-width: 100%; table-layout: fixed; overflow-wrap: break-word; word-break: break-all;\" width=\"800\">\n<thead>\n<tr>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u7c7b\u578b<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u8868\u73b0<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u4f55\u65f6\u5904\u7406<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u7f16\u8bd1\u9519\u8bef<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u6ca1\u9075\u5b88\u8bed\u6cd5\uff0c\u7f16\u4e0d\u8fc7<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u7f16\u8bd1\u9636\u6bb5\u5fc5\u987b\u6392\u9664<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u903b\u8f91\u9519\u8bef<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u80fd\u7f16\u8bd1\u80fd\u8dd1\uff0c\u7ed3\u679c\u4e0d\u5bf9<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u6d4b\u51fa\u6765\u518d\u6539\u4e1a\u52a1<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u8fd0\u884c\u65f6\u9519\u8bef<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u51fa\u73b0\u4e0d\u53ef\u80fd\u6267\u884c\u7684\u64cd\u4f5c<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u7528\u5f02\u5e38\u673a\u5236\u63a5\u4f4f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p style=\"margin: 1em 0px;\">\u5f02\u5e38\u662f\u6267\u884c\u671f\u95f4\u7684\u5404\u79cd\u610f\u5916\uff1a\u7528\u6237\u8f93\u5165\u51fa\u9519\u3001\u6587\u4ef6\u627e\u4e0d\u5230\u3001\u78c1\u76d8\u6216\u5185\u5b58\u4e0d\u591f\u3001\u6ea2\u51fa \/ \u9664\u96f6\u3001\u6570\u7ec4\u4e0b\u6807\u8d8a\u754c\u7b49\u3002\u903b\u8f91\u9519\u8bef\u6709\u65f6\u4e5f\u4f1a\u5f15\u53d1\u8fd0\u884c\u65f6\u9519\u8bef\u3002<\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e00\u3001\u8fd0\u884c\u65f6\u5e38\u89c1\u4e09\u7c7b\u5f02\u5e38<\/h2>\n<ol style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem;\">\n<li>\n<section>JVM \u5185\u90e8 Error\uff1a\u4e0d\u5728\u7528\u6237\u7a0b\u5e8f\u63a7\u5236\u4e4b\u5185\uff0c\u4e00\u822c\u4e0d\u7528\u5904\u7406\u3002<\/section>\n<\/li>\n<li>\n<section>\u7cfb\u7edf\u9884\u5b9a\u4e49\u7684\u6807\u51c6\u5f02\u5e38\uff1a\u9664\u96f6\u3001\u4e0b\u6807\u8d8a\u754c\u3001\u8bbf\u95ee\u7a7a\u5bf9\u8c61\u7b49\uff0c<strong style=\"color: #4870ac;\">\u9700\u8981\u7528\u6237\u7a0b\u5e8f\u5904\u7406<\/strong>\u3002<\/section>\n<\/li>\n<li>\n<section>\u6309\u9700\u8981\u81ea\u5b9a\u4e49\u7684\u5f02\u5e38\u7c7b\u3002<\/section>\n<\/li>\n<\/ol>\n<p style=\"margin: 1em 0px;\">\u4e0b\u9762\u8fd9\u6bb5\u4f1a\u6570\u7ec4\u8d8a\u754c\uff1a\u957f\u5ea6\u4e3a 2 \u7684\u6570\u7ec4\uff0c\u5faa\u73af\u5374\u5199\u5230\u4e0b\u6807 2\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">class TestSystemException {\n    public static void main(String[] args) {\n        int num[] = new int[2];\n        for (int i = 1; i &lt; 3; i++) {\n            num[i] = i;\n            System.out.println(\"num[\" + i + \"]=\" + i);\n        }\n    }\n}<\/code><\/pre>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e8c\u3001\u629b\u51fa\u4e0e\u6355\u6349<\/h2>\n<p style=\"margin: 1em 0px;\">\u5904\u7406\u5f02\u5e38\u5c31\u4e24\u4ef6\u4e8b\uff1a\u629b\u51fa\uff08throw\uff09\u548c\u6355\u6349\uff08catch\uff09\u3002\u8fd0\u884c\u51fa\u9519\u4e14\u80fd\u5bf9\u5e94\u5230\u67d0\u4e2a\u9884\u5b9a\u4e49\u5f02\u5e38\u7c7b\u65f6\uff0c\u7cfb\u7edf\u81ea\u52a8 new \u4e00\u4e2a\u8be5\u5f02\u5e38\u5bf9\u8c61\uff0c\u8fd9\u53eb\u629b\u51fa\u3002\u7a0b\u5e8f\u91cc\u82e5\u627e\u5230\u5904\u7406\u4ee3\u7801\uff0c\u5c31\u628a\u5bf9\u8c61\u4ea4\u7ed9\u5b83\uff0c\u8fd9\u53eb\u6355\u6349\u3002\u627e\u4e0d\u5230\u5219\u4ea4\u7ed9\u8fd0\u884c\u65f6\u9ed8\u8ba4\u5904\u7406\uff1a\u5148\u6253\u5370\u63cf\u8ff0\u5b57\u7b26\u4e32\uff0c\u518d\u7ec8\u6b62\u7a0b\u5e8f\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u629b\u51fa\u6709\u4e24\u79cd\uff1a\u7cfb\u7edf\u81ea\u52a8\u629b\u51fa\uff1b\u4eba\u4e3a\u629b\u51fa\u3002\u4eba\u4e3a\u53c8\u5206\uff1a\u65b9\u6cd5\u5934\u7528 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">throws<\/code> \u58f0\u660e\uff0c\u6216\u65b9\u6cd5\u4f53\u5185 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">throw<\/code> \u4e00\u4e2a\u5f02\u5e38\u5bf9\u8c61\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">class Throws_Exp {\n    public static void main(String[] args)\n            throws ArithmeticException, ArrayIndexOutOfBoundsException {\n        int a = 4, b = 0, c[] = {1, 2, 3, 4, 5};\n        System.out.println(a \/ b);\n        System.out.println(c[a + 1]);\n        System.out.println(\"end\");\n    }\n}\n\nclass Throw_Exp3 {\n    public static void main(String[] args) {\n        int a = 5, b = 0, c[] = {1, 2, 3, 4, 5};\n        System.out.println(\"Before throw\");\n        if (b == 0)\n            throw new ArithmeticException();\n        System.out.println(a \/ b);\n        if (a &gt; 4)\n            throw new ArrayIndexOutOfBoundsException();\n        System.out.println(a \/ b);\n    }\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u6355\u6349\u7528 try-catch\uff0c\u53ef\u9009 finally\uff08\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\u90fd\u6267\u884c\uff09\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">try {\n    \/\/ \u53ef\u80fd\u53d1\u751f\u5f02\u5e38\n} catch (\u5f02\u5e38\u7c7b1 e1) {\n    \/\/ \u5904\u7406\u5f02\u5e381\n} catch (\u5f02\u5e38\u7c7bn en) {\n    \/\/ \u5904\u7406\u5f02\u5e38n\n} finally {\n    \/\/ \u4e0d\u8bba\u5f02\u5e38\u662f\u5426\u53d1\u751f\u90fd\u8981\u6267\u884c\n}<\/code><\/pre>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e09\u3001\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b<\/h2>\n<p style=\"margin: 1em 0px;\">\u81ea\u5b9a\u4e49\u5f02\u5e38\u5fc5\u987b\u662f Throwable \u6216 Exception \u7684\u5b50\u7c7b\u3002\u4e00\u822c\u58f0\u660e\u4e24\u4e2a\u6784\u9020\u65b9\u6cd5\uff1a\u65e0\u53c2\uff0c\u4ee5\u53ca\u4ee5\u5b57\u7b26\u4e32\u4e3a\u53c2\u6570\uff08\u5b57\u7b26\u4e32\u5e94\u53cd\u6620\u5f02\u5e38\u4fe1\u606f\uff09\u3002\u81ea\u5b9a\u4e49\u5f02\u5e38\u540c\u6837\u8981\u7528 try-catch \u6355\u83b7\uff0c\u4f46\u5fc5\u987b\u7531\u7528\u6237\u81ea\u5df1 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">throw new MyException()<\/code>\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">class Exception_exp {\n    public static void main(String[] args) {\n        try {\n            System.out.println(\"2+3=\" + add(2, 3));\n            System.out.println(\"-8+10=\" + add(-8, 10));\n        } catch (Exception e) {\n            System.out.println(\"Exception is \" + e);\n        }\n    }\n    public static int add(int n, int m) throws UserException {\n        if (n &lt; 0 || m &lt; 0) throw new UserException();\n        return n + m;\n    }\n}\nclass UserException extends Exception {\n    UserException() {\n        super(\"\u6570\u636e\u4e3a\u8d1f\u6570\");\n    }\n}<\/code><\/pre>\n<blockquote style=\"display: block; font-size: 0.9em; border-left: 3px solid #4870ac; padding: 0.5em 1em; margin: 0px; background: #f6f8fa; color: #40464f;\">\n<p style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1a\u7f16\u8bd1\u9519\u8bef\u5148\u6539\u8bed\u6cd5\uff0c\u903b\u8f91\u9519\u8bef\u6539\u4e1a\u52a1\uff1b\u8fd0\u884c\u65f6\u610f\u5916\u7528 throw\/throws \u629b\u3001try-catch-finally \u63a5\uff0c\u9700\u8981\u9886\u57df\u8bed\u4e49\u5c31\u81ea\u5df1\u7ee7\u627f Exception \u518d throw\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u7a0b\u5e8f\u91cc\u7684\u9519\u8bef\u53ef\u5206\u4e09\u7c7b\uff1a\u7f16\u8bd1\u9519\u8bef\u3001\u903b\u8f91\u9519\u8bef\u548c\u8fd0\u884c\u65f6\u9519\u8bef\u3002\u5f02\u5e38\u5904\u7406\u7684\u76ee\u6807\u662f\uff1a\u5373\u4fbf\u8fd0\u884c\u4e2d\u51fa\u9519\uff0c\u4e5f\u5c3d\u91cf\u8ba9\u7a0b\u5e8f\u6b63\u5e38\u7ed3\u675f\uff0c [&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":[180],"_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":3,"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}]}}