{"id":477,"date":"2024-11-06T19:56:05","date_gmt":"2024-11-06T11:56:05","guid":{"rendered":"http:\/\/www.cmd137blog.top\/?p=477"},"modified":"2024-11-06T22:04:10","modified_gmt":"2024-11-06T14:04:10","slug":"java-%e5%bc%82%e5%b8%b8","status":"publish","type":"post","link":"http:\/\/www.cmd137blog.top\/?p=477","title":{"rendered":"JAVA-\u5f02\u5e38"},"content":{"rendered":"\n<p>Java\u4f7f\u7528\u5f02\u5e38\u6765\u8868\u793a\u9519\u8bef\uff0c\u5e76\u901a\u8fc7<code>try ... catch<\/code>\u6355\u83b7\u5f02\u5e38\uff1b<\/p>\n\n\n\n<p>Java\u7684\u5f02\u5e38\u662f\u4e00\u79cd\u7c7b\uff0c\u5e76\u4e14\u4ece<code>Throwable<\/code>\u7ee7\u627f\uff1b<\/p>\n\n\n\n<p><code>Error<\/code>\u662f\u65e0\u9700\u6355\u83b7\u7684\u4e25\u91cd\u9519\u8bef\uff0c<code>Exception<\/code>\u662f\u5e94\u8be5\u6355\u83b7\u7684\u53ef\u5904\u7406\u7684\u9519\u8bef\uff1b<\/p>\n\n\n\n<p><code>RuntimeException<\/code>\u65e0\u9700\u5f3a\u5236\u6355\u83b7\uff0c\u975e<code>RuntimeException<\/code>\u9700\u5f3a\u5236\u6355\u83b7\uff0c\u6216\u8005\u7528<code>throws<\/code>\u58f0\u660e\uff1b<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7f16\u8bd1\u65f6\u5f02\u5e38\uff1a\u6ca1\u6709\u7ee7\u627fRuntimeException\u7684\u5f02\u5e38\uff0c\u76f4\u63a5\u7ee7\u627f\u4e8e<code>Exception<\/code>\uff0c\u7f16\u8bd1\u9636\u6bb5\u5c31\u4f1a\u9519\u8bef\u63d0\u793a\u3002<\/li>\n\n\n\n<li>\u8fd0\u884c\u65f6\u5f02\u5e38\uff1aRuntimeException\u672c\u8eab\u548c\u5b50\u7c7b\u3002\u7f16\u8bd1\u9636\u6bb5\u6ca1\u6709\u9519\u8bef\u63d0\u793a\uff0c\u8fd0\u884c\u65f6\u624d\u4f1a\u51fa\u73b0\u9519\u8bef\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4e0d\u63a8\u8350\u6355\u83b7\u4e86\u5f02\u5e38\u4f46\u4e0d\u8fdb\u884c\u4efb\u4f55\u5904\u7406\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1.\u5e38\u7528\u5f02\u5e38\uff1a<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>Exception\n\u251c\u2500 RuntimeException\n\u2502  \u251c\u2500 NullPointerException\n\u2502  \u251c\u2500 IndexOutOfBoundsException\n\u2502  \u251c\u2500 SecurityException\n\u2502  \u2514\u2500 IllegalArgumentException\n\u2502     \u2514\u2500 NumberFormatException\n\u251c\u2500 IOException\n\u2502  \u251c\u2500 UnsupportedCharsetException\n\u2502  \u251c\u2500 FileNotFoundException\n\u2502  \u2514\u2500 SocketException\n\u251c\u2500 ParseException\n\u251c\u2500 GeneralSecurityException\n\u251c\u2500 SQLException\n\u2514\u2500 TimeoutException<\/strong><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2.\u6355\u83b7\u5f02\u5e38\uff1a<\/h2>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u4f5c\u7528\uff1a\u5f53\u4ee3\u7801\u51fa\u73b0\u5f02\u5e38\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u7ee7\u7eed\u5411\u4e0b\u6267\u884c\u3002\u5982\u679c\u6ca1\u6709\u6355\u83b7\uff0cJVM\u5c31\u4f1a\u56e0\u4e3a\u5f02\u5e38\u800c\u7ec8\u6b62\u7a0b\u5e8f\u3002<\/mark><\/strong><\/p>\n\n\n\n<p>\u6355\u83b7\u5f02\u5e38\u65f6\uff0c\u591a\u4e2a<code>catch<\/code>\u8bed\u53e5\u7684\u5339\u914d\u987a\u5e8f\u975e\u5e38\u91cd\u8981\uff0c\u5b50\u7c7b\u5fc5\u987b\u653e\u5728\u524d\u9762\uff1b<\/p>\n\n\n\n<p><code>finally<\/code>\u8bed\u53e5\u4fdd\u8bc1\u4e86\u6709\u65e0\u5f02\u5e38\u90fd\u4f1a\u6267\u884c\uff0c\u5b83\u662f\u53ef\u9009\u7684\uff1b<\/p>\n\n\n\n<p>\u4e00\u4e2a<code>catch<\/code>\u8bed\u53e5\u4e5f\u53ef\u4ee5\u5339\u914d\u591a\u4e2a\u975e\u7ee7\u627f\u5173\u7cfb\u7684\u5f02\u5e38\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3.NullPointerException\uff1a<\/h2>\n\n\n\n<p>\u8fd9\u662f\u6700\u5e38\u89c1\u7684\u5f02\u5e38\u3002\u7a7a\u6307\u9488\u5f02\u5e38\uff0c\u4fd7\u79f0NPE\u3002\u5982\u679c\u4e00\u4e2a\u5bf9\u8c61\u4e3a<code>null<\/code>\uff0c\u8c03\u7528\u5176\u65b9\u6cd5\u6216\u8bbf\u95ee\u5176\u5b57\u6bb5\u5c31\u4f1a\u4ea7\u751f<code>NullPointerException<\/code>\uff0c\u8fd9\u4e2a\u5f02\u5e38\u901a\u5e38\u662f\u7531JVM\u629b\u51fa\u7684\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4.\u629b\u51fa\u5f02\u5e38\uff1a<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">throws:<\/h3>\n\n\n\n<p>\u5199\u5728\u65b9\u6cd5\u5b9a\u4e49\u5904\uff0c\u8868\u793a\u58f0\u660e\u4e00\u4e2a\u5f02\u5e38\uff0c\u544a\u8bc9\u8c03\u7528\u8005\uff0c\u4f7f\u7528\u672c\u65b9\u6cd5\u53ef\u80fd\u4f1a\u6709\u54ea\u4e9b\u5f02\u5e38\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7f16\u8bd1\u65f6\u5f02\u5e38\uff1a\u5fc5\u987b\u8981\u5199<\/li>\n\n\n\n<li>\u8fd0\u884c\u65f6\u5f02\u5e38\uff1a\u53ef\u4ee5\u4e0d\u5199<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>public void Fname()throws \u5f02\u5e38\u540d1\uff0c\u5f02\u5e38\u540d2...{\n    ....\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">throw:<\/h3>\n\n\n\n<p>\u5199\u5728\u65b9\u6cd5\u5185\uff0c\u624b\u52a8\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n\n\n\n<p>\u5f53\u67d0\u4e2a\u65b9\u6cd5\u629b\u51fa\u4e86\u5f02\u5e38\u65f6\uff0c\u5982\u679c\u5f53\u524d\u65b9\u6cd5\u6ca1\u6709\u6355\u83b7\u5f02\u5e38\uff0c\u5f02\u5e38\u5c31\u4f1a\u88ab\u629b\u5230\u4e0a\u5c42\u8c03\u7528\u65b9\u6cd5\uff0c\u76f4\u5230\u9047\u5230\u67d0\u4e2a<code>try ... catch<\/code>\u88ab\u6355\u83b7\u4e3a\u6b62\uff1a<\/p>\n\n\n\n<p>\u793a\u4f8b1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Main {\n    public static void main(String&#91;] args) {\n        try {\n            process1();\n        } catch (Exception e1) {\n            e.printStackTrace();\n        }\n    }\n\n    static void process1() {\n        try {\n            process2();\n        } catch (NullPointerException e2) {\n            throw new IllegalArgumentException();\n        }\n    }\n\n    static void process2() {\n        throw new NullPointerException();\n    }<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>java.lang.IllegalArgumentException<br>at ExceptionTest.Test1.process1(Test1.java:16)<br>at ExceptionTest.Test1.main(Test1.java:6)<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u8f6c\u6362\u5f02\u5e38\uff1a<\/h3>\n<\/blockquote>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff1aprocess2()\u4e2d\u7684NullPointerException\u5f02\u5e38\u88ab\u629b\u51fa\u5230\u4e0a\u5c42process1()\u4e2d\uff0cprocess1()\u4e2d\u6355\u83b7\u52302\u4e2d\u5f02\u5e38\uff0c\u4ea7\u751f\u7684\u5f02\u5e38IllegalArgumentException\u88ab\u629b\u51fa\u5230\u4e0a\u5c42main\u4e2d\uff0c\u88ab\u6355\u83b7\u3002\u76f8\u5f53\u4e8e<strong><em><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">\u8f6c\u6362\u5f02\u5e38<\/mark><\/em><\/strong>\uff1b<\/p>\n\n\n\n<p>\u6700\u539f\u59cb\u7684\u5f02\u5e38\u4fe1\u606f\u88ab\u4e22\u5931\u4e86\uff0c\u800c\u8fd9\u5e76\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>\u8981\u5f97\u5230\u5b8c\u6574\u7684\u5f02\u5e38\u4fe1\u606f\uff0c\u53ef\u4ee5\u5c06process1()\u4e2dcatch\u5230\u7684e2\u518d\u4f20\u5165\u5230p1\u4e2d\u6784\u9020\u7684\u5f02\u5e38\uff1aIllegalArgumentException()\u4e2d\u3002<\/p>\n\n\n\n<p>\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static void process1(){\n        try {\n            process2();\n        }catch (NullPointerException e2){\n            throw new IllegalArgumentException(e2);\/\/\u5c06p1()\u4e2dcatch\u5230\u7684e2\u518d\u4f20\u5165\u5230p1\u4e2d\u6784\u9020\u7684\u5f02\u5e38\n        }\n    }<\/code><\/pre>\n\n\n\n<p>\u5f97\u5230\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>java.lang.IllegalArgumentException: java.lang.NullPointerException<br>at ExceptionTest.Test1.process1(Test1.java:16)<br>at ExceptionTest.Test1.main(Test1.java:6)<br>Caused by: java.lang.NullPointerException<br>at ExceptionTest.Test1.process2(Test1.java:21)<br>at ExceptionTest.Test1.process1(Test1.java:14)<br>\u2026 1 more<\/p>\n<\/blockquote>\n\n\n\n<p><strong><em>\u6709\u4e86\u5b8c\u6574\u7684\u5f02\u5e38\u6808\u7684\u4fe1\u606f\uff0c\u6211\u4eec\u624d\u80fd\u5feb\u901f\u5b9a\u4f4d\u5e76\u4fee\u590d\u4ee3\u7801\u7684\u95ee\u9898\u3002<\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5.\u6848\u4f8b\uff1a<\/h2>\n\n\n\n<p>\u8f93\u5165\u4e00\u4e2aStudent\u7c7b\u5bf9\u8c61\u7684\u4fe1\u606f\uff0c\u8981\u6c42\uff1a\u59d3\u540d\u57282-4\u4e2a\u5b57\u7b26\uff1b\u5e74\u9f84\u572816-22\u5c81\u4e4b\u95f4\u3002<\/p>\n\n\n\n<p>\u5206\u6790\uff1a\u5bf9\u4e8e\u4e00\u6b21\u8f93\u51fa\uff0c\u6211\u4eec\u53ef\u4ee5\u8fdb\u884cif\u5224\u65ad\u6765\u5b8c\u6210\u3002\u4f46\u5982\u679c\u5728\u5176\u4ed6\u5730\u65b9\u4e5f\u8981\u4f7f\u7528\u8fd9\u4e2aStudent\u7c7b\u8fdb\u884c\u5bf9\u8c61\u7684\u521b\u5efa\u5462\uff0c\u5c31\u5f97\u6bcf\u6b21\u90fd\u5199\u5224\u65ad\u3002\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5c06\u5224\u65ad\u6761\u4ef6\u5199\u5728set\u65b9\u6cd5\uff0c\u82e5\u4e0d\u7b26\u5408\u6761\u4ef6\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>Student.java<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package ExceptionTest;\n\npublic class Student {\n    private String name;\n    private int age;\n\n...\n\n    public void setName(String name) {\n        if (name.length()>=5||name.length()&lt;=1)\n            throw new RuntimeException();\n        this.name = name;\n    }\n\n...\n\n    public void setAge(int age) {\n        if(age&lt;16||age>22)\n            throw new RuntimeException();\n        this.age = age;\n    }\n\n  ...\n}\n<\/code><\/pre>\n\n\n\n<p>test\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static void main(String&#91;] args) {\n\n        Scanner scanner=new Scanner(System.in);\n        Student student =new Student();\n\n\n        while (true) {\n            try {\n                System.out.println(\"\u8bf7\u8f93\u5165\u59d3\u540d\");\n                String name=scanner.nextLine();\n                student.setName(name);\n\n                System.out.println(\"\u8bf7\u8f93\u5165\u5e74\u9f84\");\n                String strage=scanner.nextLine();\n                int age =Integer.parseInt(strage);\n                student.setAge(age);\n\n                break;\n            } catch (NumberFormatException e) {\n                \/\/e.printStackTrace();\n                System.out.println(\"\u5e74\u9f84\u8f93\u5165\u6709\u8bef\u6216\u4e0d\u5408\u6cd5\");\n            } catch (RuntimeException e){\n                \/\/e.printStackTrace();\n                System.out.println(\"\u59d3\u540d\u8f93\u5165\u6709\u8bef\u6216\u4e0d\u5408\u6cd5\");\n            }\n        }\n\n        System.out.println(student);\n    }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Java\u4f7f\u7528\u5f02\u5e38\u6765\u8868\u793a\u9519\u8bef\uff0c\u5e76\u901a\u8fc7try &#8230; catch\u6355\u83b7\u5f02\u5e38\uff1b Java\u7684\u5f02\u5e38\u662f\u4e00\u79cd\u7c7b\uff0c\u5e76\u4e14\u4eceThro [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[56],"tags":[25],"class_list":["post-477","post","type-post","status-publish","format-standard","hentry","category-java","tag-java"],"_links":{"self":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/477","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=477"}],"version-history":[{"count":2,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/477\/revisions"}],"predecessor-version":[{"id":482,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/477\/revisions\/482"}],"wp:attachment":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=477"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}