{"id":2224,"date":"2025-03-02T10:43:48","date_gmt":"2025-03-02T02:43:48","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2224"},"modified":"2025-03-02T10:43:48","modified_gmt":"2025-03-02T02:43:48","slug":"java-%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/02\/java-%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86\/","title":{"rendered":"Java \u5f02\u5e38\u5904\u7406"},"content":{"rendered":"\n<p>Java \u5f02\u5e38\u5904\u7406\u673a\u5236\u63d0\u4f9b\u4e86\u4e00\u79cd\u5f3a\u5927\u7684\u673a\u5236\u6765\u5904\u7406\u8fd0\u884c\u65f6\u9519\u8bef\uff0c\u4ece\u800c\u907f\u514d\u7a0b\u5e8f\u7684\u5d29\u6e83\u5e76\u4e14\u80fd\u591f\u4f18\u96c5\u5730\u5904\u7406\u9519\u8bef\u3002Java \u4e2d\u7684\u5f02\u5e38\u5904\u7406\u7531 <code>try<\/code>\u3001<code>catch<\/code>\u3001<code>finally<\/code> \u8bed\u53e5\u7ec4\u6210\uff0c\u901a\u8fc7\u5b83\u4eec\u80fd\u591f\u6355\u83b7\u5e76\u5904\u7406\u8fd0\u884c\u65f6\u53d1\u751f\u7684\u5f02\u5e38\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. \u5f02\u5e38\u7684\u5206\u7c7b<\/strong><\/h3>\n\n\n\n<p>Java \u4e2d\u7684\u5f02\u5e38\u5206\u4e3a\u4e24\u7c7b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5df2\u68c0\u67e5\u5f02\u5e38 (Checked Exception)<\/strong>: \u7f16\u8bd1\u5668\u5f3a\u5236\u8981\u6c42\u5904\u7406\u7684\u5f02\u5e38\uff0c\u901a\u5e38\u662f\u7531\u4e8e\u5916\u90e8\u73af\u5883\u95ee\u9898\u5bfc\u81f4\u7684\uff0c\u5982\u6587\u4ef6\u672a\u627e\u5230\u3001\u7f51\u7edc\u8fde\u63a5\u4e2d\u65ad\u7b49\u3002\u5fc5\u987b\u5728\u4ee3\u7801\u4e2d\u663e\u5f0f\u5904\u7406\uff08\u4f7f\u7528 <code>try-catch<\/code>\uff09\u6216\u8005\u58f0\u660e\uff08\u4f7f\u7528 <code>throws<\/code>\uff09\u3002 \u4f8b\u5b50\uff1a<code>IOException<\/code>\u3001<code>SQLException<\/code><\/li>\n\n\n\n<li><strong>\u672a\u68c0\u67e5\u5f02\u5e38 (Unchecked Exception)<\/strong>: \u4e5f\u53eb\u8fd0\u884c\u65f6\u5f02\u5e38\uff0c\u662f\u7531\u7a0b\u5e8f\u5458\u7684\u9519\u8bef\u6216\u903b\u8f91\u95ee\u9898\u5f15\u8d77\u7684\uff0c\u7f16\u8bd1\u5668\u4e0d\u5f3a\u5236\u8981\u6c42\u5904\u7406\u3002\u8fd0\u884c\u65f6\u5f02\u5e38\u901a\u5e38\u4e0d\u9700\u8981\u663e\u5f0f\u5904\u7406\u3002 \u4f8b\u5b50\uff1a<code>NullPointerException<\/code>\u3001<code>ArrayIndexOutOfBoundsException<\/code><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. \u5f02\u5e38\u7684\u57fa\u672c\u5904\u7406<\/strong><\/h3>\n\n\n\n<p>Java \u4f7f\u7528 <code>try-catch<\/code> \u5757\u6765\u5904\u7406\u5f02\u5e38\u3002\u5f53\u4ee3\u7801\u4e2d\u53ef\u80fd\u53d1\u751f\u5f02\u5e38\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>try<\/code> \u5757\u5305\u56f4\u8fd9\u4e9b\u4ee3\u7801\uff0c\u5e76\u4f7f\u7528 <code>catch<\/code> \u5757\u6355\u83b7\u5f02\u5e38\u8fdb\u884c\u5904\u7406\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.1 \u4f7f\u7528 try-catch \u5757<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ExceptionExample {\n    public static void main(String&#91;] args) {\n        try {\n            int result = 10 \/ 0;  \/\/ \u8fd9\u91cc\u4f1a\u629b\u51fa\u4e00\u4e2a ArithmeticException\n        } catch (ArithmeticException e) {\n            System.out.println(\"\u9519\u8bef\uff1a\u9664\u6570\u4e0d\u80fd\u4e3a\u96f6\uff01\");\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>try<\/code> \u5757\u4e2d\u7684\u4ee3\u7801\u4f1a\u6267\u884c\uff0c\u5982\u679c\u53d1\u751f\u5f02\u5e38\uff0c\u5219\u8df3\u8f6c\u5230\u5bf9\u5e94\u7684 <code>catch<\/code> \u5757\u8fdb\u884c\u5f02\u5e38\u5904\u7406\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.2 \u591a\u4e2a catch \u5757<\/strong><\/h4>\n\n\n\n<p>\u4e00\u4e2a <code>try<\/code> \u5757\u53ef\u4ee5\u6709\u591a\u4e2a <code>catch<\/code> \u5757\u6765\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class MultipleCatchExample {\n    public static void main(String&#91;] args) {\n        try {\n            int&#91;] numbers = new int&#91;2];\n            numbers&#91;10] = 25;  \/\/ \u8fd9\u91cc\u4f1a\u629b\u51fa ArrayIndexOutOfBoundsException\n        } catch (ArithmeticException e) {\n            System.out.println(\"\u7b97\u672f\u5f02\u5e38\uff01\");\n        } catch (ArrayIndexOutOfBoundsException e) {\n            System.out.println(\"\u6570\u7ec4\u4e0b\u6807\u8d8a\u754c\uff01\");\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.3 \u6355\u83b7\u591a\u4e2a\u5f02\u5e38<\/strong><\/h4>\n\n\n\n<p>\u5728 Java 7 \u53ca\u4ee5\u540e\u7248\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u7ba1\u9053\u7b26\uff08<code>|<\/code>\uff09\u6765\u6355\u83b7\u591a\u4e2a\u5f02\u5e38\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class MultipleExceptionExample {\n    public static void main(String&#91;] args) {\n        try {\n            int result = 10 \/ 0;  \/\/ ArithmeticException\n            int&#91;] numbers = new int&#91;2];\n            numbers&#91;10] = 25;  \/\/ ArrayIndexOutOfBoundsException\n        } catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {\n            System.out.println(\"\u53d1\u751f\u4e86\u5f02\u5e38\uff1a\" + e);\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.4 finally \u5757<\/strong><\/h4>\n\n\n\n<p>\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0c<code>finally<\/code> \u5757\u4e2d\u7684\u4ee3\u7801\u603b\u4f1a\u6267\u884c\u3002<code>finally<\/code> \u5757\u901a\u5e38\u7528\u4e8e\u6e05\u7406\u8d44\u6e90\uff08\u5982\u5173\u95ed\u6587\u4ef6\u3001\u6570\u636e\u5e93\u8fde\u63a5\u7b49\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class FinallyExample {\n    public static void main(String&#91;] args) {\n        try {\n            int result = 10 \/ 0;  \/\/ \u4f1a\u629b\u51fa ArithmeticException\n        } catch (ArithmeticException e) {\n            System.out.println(\"\u9519\u8bef\uff1a\u9664\u6570\u4e0d\u80fd\u4e3a\u96f6\uff01\");\n        } finally {\n            System.out.println(\"\u65e0\u8bba\u53d1\u751f\u4ec0\u4e48\uff0cfinally \u5757\u603b\u4f1a\u6267\u884c\uff01\");\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.5 \u4f7f\u7528 throw \u629b\u51fa\u5f02\u5e38<\/strong><\/h4>\n\n\n\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>throw<\/code> \u5173\u952e\u5b57\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u5bf9\u8c61\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ThrowExample {\n    public static void main(String&#91;] args) {\n        try {\n            validateAge(15);\n        } catch (IllegalArgumentException e) {\n            System.out.println(e.getMessage());\n        }\n    }\n\n    public static void validateAge(int age) {\n        if (age &lt; 18) {\n            throw new IllegalArgumentException(\"\u5e74\u9f84\u5fc5\u987b\u5927\u4e8e\u7b49\u4e8e 18 \u5c81\uff01\");\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53\u5e74\u9f84\u5c0f\u4e8e 18 \u65f6\uff0c\u629b\u51fa\u4e86\u4e00\u4e2a <code>IllegalArgumentException<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.6 \u81ea\u5b9a\u4e49\u5f02\u5e38<\/strong><\/h4>\n\n\n\n<p>Java \u5141\u8bb8\u4f60\u521b\u5efa\u81ea\u5df1\u7684\u5f02\u5e38\u7c7b\uff0c\u7ee7\u627f <code>Exception<\/code> \u6216 <code>RuntimeException<\/code> \u7c7b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u81ea\u5b9a\u4e49\u4e00\u4e2a\u5f02\u5e38\u7c7b\nclass AgeNotValidException extends Exception {\n    public AgeNotValidException(String message) {\n        super(message);\n    }\n}\n\npublic class CustomExceptionExample {\n    public static void main(String&#91;] args) {\n        try {\n            validateAge(15);\n        } catch (AgeNotValidException e) {\n            System.out.println(e.getMessage());\n        }\n    }\n\n    public static void validateAge(int age) throws AgeNotValidException {\n        if (age &lt; 18) {\n            throw new AgeNotValidException(\"\u5e74\u9f84\u4e0d\u7b26\u5408\u8981\u6c42\uff0c\u5fc5\u987b\u5927\u4e8e\u7b49\u4e8e 18 \u5c81\uff01\");\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\uff0c<code>AgeNotValidException<\/code> \u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5f02\u5e38\uff0c\u7528\u4e8e\u68c0\u67e5\u5e74\u9f84\u662f\u5426\u7b26\u5408\u8981\u6c42\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. \u5f02\u5e38\u58f0\u660e &#8211; throws<\/strong><\/h3>\n\n\n\n<p>\u6709\u65f6\u4f60\u53ef\u80fd\u5e0c\u671b\u5c06\u5f02\u5e38\u7684\u5904\u7406\u4ea4\u7ed9\u8c03\u7528\u8005\u6765\u5904\u7406\u3002\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528 <code>throws<\/code> \u5173\u952e\u5b57\u5728\u65b9\u6cd5\u7b7e\u540d\u4e2d\u58f0\u660e\u5f02\u5e38\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.IOException;\n\npublic class ThrowsExample {\n    public static void main(String&#91;] args) {\n        try {\n            readFile(\"file.txt\");\n        } catch (IOException e) {\n            System.out.println(\"\u8bfb\u53d6\u6587\u4ef6\u65f6\u53d1\u751f\u9519\u8bef\uff1a\" + e.getMessage());\n        }\n    }\n\n    \/\/ \u5728\u65b9\u6cd5\u7b7e\u540d\u4e2d\u58f0\u660e\u629b\u51fa\u5f02\u5e38\n    public static void readFile(String fileName) throws IOException {\n        if (fileName == null) {\n            throw new IOException(\"\u6587\u4ef6\u540d\u4e0d\u80fd\u4e3a\u7a7a\uff01\");\n        }\n        \/\/ \u8bfb\u53d6\u6587\u4ef6\u7684\u4ee3\u7801...\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>readFile<\/code> \u65b9\u6cd5\u58f0\u660e\u629b\u51fa <code>IOException<\/code> \u5f02\u5e38\uff0c\u8c03\u7528\u8be5\u65b9\u6cd5\u65f6\u5fc5\u987b\u5904\u7406\u8be5\u5f02\u5e38\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. \u5f02\u5e38\u7684\u5c42\u6b21\u7ed3\u6784<\/strong><\/h3>\n\n\n\n<p>Java \u7684\u5f02\u5e38\u7c7b\u6709\u4e00\u4e2a\u5c42\u6b21\u7ed3\u6784\uff0c\u6700\u57fa\u672c\u7684\u7c7b\u662f <code>Throwable<\/code>\uff0c\u5b83\u6709\u4e24\u4e2a\u4e3b\u8981\u7684\u5b50\u7c7b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Error<\/strong>: \u4ee3\u8868 JVM \u5c42\u7ea7\u7684\u9519\u8bef\uff08\u4f8b\u5982\u5185\u5b58\u6ea2\u51fa\u3001\u5806\u6808\u6ea2\u51fa\u7b49\uff09\uff0c\u8fd9\u4e9b\u9519\u8bef\u901a\u5e38\u65e0\u6cd5\u6062\u590d\uff0c\u7a0b\u5e8f\u4e5f\u6ca1\u6709\u80fd\u529b\u5904\u7406\u3002<\/li>\n\n\n\n<li><strong>Exception<\/strong>: \u4ee3\u8868\u7a0b\u5e8f\u7ea7\u7684\u5f02\u5e38\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u6355\u83b7\u5e76\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002<\/li>\n<\/ul>\n\n\n\n<p><code>Exception<\/code> \u53c8\u6709\u4e24\u4e2a\u5b50\u7c7b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>CheckedException<\/strong>: \u7f16\u8bd1\u65f6\u5f02\u5e38\uff0c\u9700\u8981\u663e\u5f0f\u5904\u7406\u3002<\/li>\n\n\n\n<li><strong>UncheckedException<\/strong>: \u8fd0\u884c\u65f6\u5f02\u5e38\uff0c\u53ef\u4ee5\u4e0d\u5904\u7406\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. \u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>try-catch<\/code> \u5757\u6765\u6355\u83b7\u5e76\u5904\u7406\u5f02\u5e38\u3002<\/li>\n\n\n\n<li><code>finally<\/code> \u5757\u7528\u4e8e\u6267\u884c\u6e05\u7406\u5de5\u4f5c\uff0c\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\u3002<\/li>\n\n\n\n<li><code>throw<\/code> \u5173\u952e\u5b57\u7528\u4e8e\u629b\u51fa\u5f02\u5e38\uff0c<code>throws<\/code> \u5173\u952e\u5b57\u7528\u4e8e\u58f0\u660e\u5f02\u5e38\u3002<\/li>\n\n\n\n<li>\u53ef\u4ee5\u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>catch<\/code> \u6355\u83b7\u591a\u4e2a\u5f02\u5e38\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>|<\/code> \u7b26\u53f7\u7b80\u5316\u4ee3\u7801\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0cJava \u63d0\u4f9b\u4e86\u4e00\u79cd\u7ed3\u6784\u5316\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u8ba9\u6211\u4eec\u80fd\u591f\u7f16\u5199\u66f4\u52a0\u5065\u58ee\u548c\u53ef\u7ef4\u62a4\u7684\u4ee3\u7801\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java \u5f02\u5e38\u5904\u7406\u673a\u5236\u63d0\u4f9b\u4e86\u4e00\u79cd\u5f3a\u5927\u7684\u673a\u5236\u6765\u5904\u7406\u8fd0\u884c\u65f6\u9519\u8bef\uff0c\u4ece\u800c\u907f\u514d\u7a0b\u5e8f\u7684\u5d29\u6e83\u5e76\u4e14\u80fd\u591f\u4f18\u96c5\u5730\u5904\u7406\u9519\u8bef\u3002Java [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[68],"tags":[],"class_list":["post-2224","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2224","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2224"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2224\/revisions"}],"predecessor-version":[{"id":2225,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2224\/revisions\/2225"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}