
{"id":2190,"date":"2020-08-26T15:40:29","date_gmt":"2020-08-26T07:40:29","guid":{"rendered":"http:\/\/127.0.0.2\/?p=2190"},"modified":"2020-08-27T13:52:54","modified_gmt":"2020-08-27T05:52:54","slug":"2020java%e6%95%99%e7%a8%8b%ef%bc%9a%e5%bc%82%e5%b8%b8","status":"publish","type":"post","link":"https:\/\/www.9713job.com\/?p=2190","title":{"rendered":"2020java\u6559\u7a0b\uff1a\u5f02\u5e38"},"content":{"rendered":"<h3>\u5f02\u5e38<\/h3>\n<p><strong>\u4ec0\u4e48\u662f\u5f02\u5e38\uff1f<\/strong><br \/>\n\u6982\u5ff5\uff1a\u7a0b\u5e8f\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u7684\u7279\u6b8a\u60c5\u51b5\u3002<br \/>\n\u5f02\u5e38\u5904\u7406\u7684\u5fc5\u8981\u6027\uff1a\u4efb\u4f55\u7a0b\u5e8f\u90fd\u53ef\u80fd\u5b58\u5728\u5927\u91cf\u7684\u4f4d\u7f6e\u95ee\u9898\u3001\u9519\u8bef\uff0c\u5982\u679c\u4e0d\u5bf9\u8fd9\u4e9b\u95ee\u9898\u8fdb\u884c\u6b63\u786e\u5904\u7406\uff0c\u5219\u53ef\u80fd\u7a0b\u5e8f\u4e2d\u65ad\uff0c\u9020\u6210\u4e0d\u5fc5\u8981\u7684\u635f\u5931\u3002<\/p>\n<p><strong>\u5f02\u5e38\u7684\u5206\u7c7b<\/strong><br \/>\nThrowable:\u53ef\u629b\u7684\uff0c\u4e00\u5207\u9519\u8bef\u6216\u5f02\u5e38\u7684\u7236\u7c7b\uff0c\u4f4d\u4e8ejava.lang\u5305\u4e2d<br \/>\n|-Error\uff1aJVM\u3001\u786c\u4ef6\u3001\u6267\u884c\u903b\u8f91\u9519\u8bef\uff0c\u4e0d\u80fd\u624b\u52a8\u5904\u7406<br \/>\n|-Exception\uff1a\u7a0b\u5e8f\u5728\u8fd0\u884c\u548c\u914d\u7f6e\u4e2d\u4ea7\u751f\u7684\u95ee\u9898\uff0c\u53ef\u5904\u7406\u3002<br \/>\n||-RuntimeException\uff1a\u8fd0\u884c\u65f6\u5f02\u5e38\uff0c\u53ef\u5904\u7406\uff0c\u53ef\u4e0d\u5904\u7406<br \/>\n||-CheckedException\uff1a\u53d7\u67e5\u5f02\u5e38\uff0c\u5fc5\u987b\u5904\u7406<!--more--><\/p>\n<p><strong>\u5f02\u5e38\u7684\u4ea7\u751f<\/strong><br \/>\n\u81ea\u52a8\u629b\u51fa\u5f02\u5e38\uff1a\u5f53\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u9047\u5230\u4e0d\u7b26\u5408\u89c4\u8303\u7684\u4ee3\u7801\u6216\u7ed3\u679c\u65f6\uff0c\u4f1a\u4ea7\u751f\u5f02\u5e38<br \/>\n\u624b\u52a8\u629b\u51fa\u5f02\u5e38\uff1a\u8bed\u6cd5\uff1athrow new \u5f02\u5e38\u7c7b\u578b(&#8220;\u5b9e\u9645\u53c2\u6570&#8221;)<br \/>\n\u4ea7\u751f\u5f02\u5e38\u7ed3\u679c\uff1a\u76f8\u5f53\u4e8e\u9047\u5230 return\u8bed\u53e5\uff0c\u5bfc\u81f4\u7a0b\u5e8f\u56e0\u5f02\u5e38\u800c\u7ec8\u6b62<\/p>\n<p><strong>\u5f02\u5e38\u7684\u4f20\u9012<\/strong><br \/>\n\u5f02\u5e38\u7684\u4f20\u9012\uff1a\u6309\u7167\u65b9\u6cd5 \u8c03\u7528\u94fe\u53cd\u5411\u4f20\u9012\uff0c\u5982\u59cb\u7ec8\u6ca1\u6709\u5904\u7406\u5f02\u5e38\uff0c\u6700\u7ec8\u4f1a\u7531JVM\u8fdb\u884c\u9ed8\u8ba4\u5f02\u5e38\u5904\u7406(\u5806\u6808\u8ddf\u8e2a\u4fe1\u606f)<br \/>\n\u53d7\u67e5\u5f02\u5e38\uff1athrows \u58f0\u660e\u5f02\u5e38\uff0c\u4fee\u9970\u5728\u65b9\u6cd5\u53c2\u6570\u5217\u8868\u540e\u7aef<br \/>\n\u8fd0\u884c\u65f6\u5f02\u5e38\uff1a\u56e0\u53ef\u5904\u7406\u53ef\u4e0d\u5904\u7406\uff0c\u65e0\u5e8f\u58f0\u660e\u5f02\u5e38<\/p>\n<pre>public class demos{\r\n    public static void main(String[] args) {\r\n        m1(2);\r\n    }\r\n    public static void m1(int n){\r\n        System.out.println(\"m1()-start\");\r\n        m2(n);\r\n        System.out.println(\"m1()-end\");\r\n    }\r\n    public static void m2(int n){\r\n        System.out.println(\"m2()-start\");\r\n        m3(n);\r\n        System.out.println(\"m2()-end\");\r\n    }\r\n    public static void m3(int n){\r\n        System.out.println(\"m3()-start\");\r\n        if(n%2!=0){\r\n            System.out.println(\"m3()-end\");\r\n        }else{\r\n            throw new RuntimeException();\r\n        }\r\n    }\r\n}<\/pre>\n<p><strong>\u5f02\u5e38\u7684\u5904\u7406<\/strong><\/p>\n<pre>import java.util.*;\r\npublic class demos{\r\n    public static void main(String[] args) {\r\n        Scanner sc=new Scanner(System.in);\r\n        int a=0,b=0,result=0;\r\n        try{\r\n            \/\/\u53ef\u80fd\u51fa\u73b0\u5f02\u5e38\u7684\u4ee3\u7801\r\n            System.out.print(\"\u8bf7\u8f93\u5165\u88ab\u9664\u6570:\");\r\n            a=sc.nextInt();\r\n            System.out.print(\"\u8bf7\u8f93\u5165\u9664\u6570:\");\r\n            b=sc.nextInt();\r\n            result=a\/b;\r\n        }catch (ArithmeticException e){\r\n            \/\/\u5f02\u5e38\u5904\u7406\u7684\u76f8\u5173\u4ee3\u7801\uff0c\u5982 e.getMessage()\u3001e.printStackTrace()\r\n            System.out.println(\"\u7b97\u6570\u5f02\u5e38\");\r\n        }catch (InputMismatchException e){\r\n            System.out.println(\"\u8f93\u5165\u6709\u8bef\");\r\n        }finally {\r\n            \/\/\u65e0\u8bba\u662f\u5426\u51fa\u73b0\u5f02\u5e38\uff0c\u90fd\u9700\u6267\u884c\u7684\u4ee3\u7801\u7ed3\u6784\uff0c\u5e38\u7528\u4e8e\u91ca\u653e\u8d44\u6e90\r\n            System.out.println(result);\r\n        }\r\n    }\r\n}<\/pre>\n<p><strong>\u5f02\u5e38\u5904\u7406\u7ed3\u6784<\/strong><br \/>\ntry{}catch{}<br \/>\ntry{}catch{}catch{}<br \/>\ntry{}catch{}finally{}<br \/>\ntry{}catch{}catch{}finally{}<br \/>\ntry{}finally{}<br \/>\n\u6ce8\uff1a\u591a\u91cdcatch\uff0c\u9075\u5faa\u4ece\u5b50\u5230\u7236\u7684\u987a\u5e8f\uff0c\u7236\u7c7b\u5f02\u5e38\u5728\u6700\u540e<\/p>\n<p><strong>\u81ea\u5b9a\u4e49\u5f02\u5e38<\/strong><br \/>\n\u9700\u7ee7\u627f\u81eaException\u6216Exception\u5b50\u7c7b\uff0c\u5e38\u7528\u4e8eRuntimeException<br \/>\n\u5fc5\u8981\u63d0\u4f9b\u7684\u6784\u9020\u65b9\u6cd5\uff1a<br \/>\n\u65e0\u60e8\u6784\u9020\u65b9\u6cd5<br \/>\nString message\u53c2\u6570\u7684\u6784\u9020\u65b9\u6cd5<\/p>\n<pre>import java.util.*;\r\n\r\npublic class demos{\r\n    public static void main(String[] args) {\r\n        Student s1=new Student();\r\n        Scanner sc=new Scanner(System.in);\r\n        try{\r\n            System.out.print(\"\u8bf7\u8f93\u5165\u59d3\u540d:\");\r\n            String name=sc.next();\r\n            s1.setName(name);\r\n            System.out.print(\"\u8bf7\u8f93\u5165\u6027\u522b:\");\r\n            String sex=sc.next();\r\n            s1.setSex(sex);\r\n            System.out.print(\"\u8bf7\u8f93\u5165\u5e74\u9f84:\");\r\n            int age=sc.nextInt();\r\n            s1.setAge(age);\r\n        }catch (InputMismatchException e){\r\n            System.out.println(\"\u8f93\u5165\u5f02\u5e38\");\r\n        }catch (AgeException | SexException e){\r\n            System.out.println(e.getMessage());\r\n        }finally {\r\n            System.out.println(s1);\r\n        }\r\n    }\r\n}\r\nclass Student{\r\n    private String name;\r\n    private String sex;\r\n    private Integer age;\r\n    public Student(){}\r\n    public Student(String name, String sex, Integer age) {\r\n        this.name = name;\r\n        this.sex = sex;\r\n        this.age = age;\r\n    }\r\n    public String getName() {\r\n        return name;\r\n    }\r\n    public void setName(String name) {\r\n        this.name = name;\r\n    }\r\n    public String getSex() {\r\n        return sex;\r\n    }\r\n    public void setSex(String sex) throws SexException {\r\n        if(\"\u7537\".equals(sex)||\"\u5973\".equals(sex)){\r\n            this.sex=sex;\r\n        }else{\r\n            throw new SexException(\"\u6027\u522b\u6709\u8bef\");\r\n        }\r\n    }\r\n    public Integer getAge() {\r\n        return age;\r\n    }\r\n    public void setAge(Integer age) throws AgeException {\r\n        if(age&gt;100||age&lt;1){\r\n            throw new AgeException(\"\u5e74\u9f84\u6709\u8bef\");\r\n        }else this.age=age;\r\n    }\r\n    @Override\r\n    public String toString() {\r\n        return \"Student{\" +\r\n                \"name='\" + name + '\\'' +\r\n                \", sex='\" + sex + '\\'' +\r\n                \", age=\" + age +\r\n                '}';\r\n    }\r\n}\r\n\/\/\u6027\u522b\u5f02\u5e38\r\nclass SexException extends Exception{\r\n    public SexException() {\r\n    }\r\n    public SexException(String message) {\r\n        super(message);\r\n    }\r\n}\r\n\/\/\u5e74\u9f84\u5f02\u5e38\r\nclass AgeException extends Exception{\r\n    public AgeException() {\r\n    }\r\n    public AgeException(String message) {\r\n        super(message);\r\n    }\r\n}<\/pre>\n<p>\u65b9\u6cd5\u8986\u76d6<br \/>\n\u5e26\u6709\u5f02\u5e38\u58f0\u660e\u7684\u65b9\u6cd5\u8986\u76d6\uff1a<br \/>\n-\u65b9\u6cd5\u540d\u3001\u53c2\u6570\u5217\u8868\u3001\u8fd4\u56de\u503c\u7c7b\u578b\u5fc5\u987b\u548c\u7236\u7c7b\u76f8\u540c<br \/>\n-\u5b50\u7c7b\u7684\u8bbf\u95ee\u4fee\u9970\u7b26\u548c\u7236\u7c7b\u76f8\u540c\u6216\u662f\u6bd4\u66f4\u5bbd<br \/>\n-\u5b50\u7c7b\u4e2d\u7684\u65b9\u6cd5\uff0c\u4e0d\u80fd\u629b\u51fa\u6bd4\u7236\u7c7b\u66f4\u5bbd\u7684\u5f02\u5e38<\/p>\n<pre>public class demos{\r\n    public static void main(String[] args) {\r\n        Super s=new Sub();\r\n        try {\r\n            s.method();\r\n        } catch (Exception e) {\r\n            e.printStackTrace();\r\n        }\r\n    }\r\n}\r\nclass Super{\r\n    \/\/\u7236\u7c7b\u629b\u51fa\u7684\u5f02\u5e38\uff0c\u5b50\u7c7b\u629b\u7684\u5f02\u5e38\u5c0f\u4e8e\u7b49\u4e8e\u7236\u7c7b\u7684\u5f02\u5e38\r\n    public void method() throws Exception{\r\n        System.out.println(\"Super - method()\");\r\n    }\r\n}\r\nclass Sub extends Super{\r\n    @Override\r\n    public void method(){\r\n        System.out.println(\"Super - method()\");\r\n    }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5f02\u5e38 \u4ec0\u4e48\u662f\u5f02\u5e38\uff1f \u6982\u5ff5\uff1a\u7a0b\u5e8f\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u7684\u7279\u6b8a\u60c5\u51b5\u3002 \u5f02\u5e38\u5904\u7406\u7684\u5fc5\u8981\u6027\uff1a\u4efb\u4f55\u7a0b\u5e8f\u90fd\u53ef\u80fd\u5b58\u5728\u5927\u91cf\u7684\u4f4d\u7f6e\u95ee\u9898 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-2190","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2190","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2190"}],"version-history":[{"count":5,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2190\/revisions"}],"predecessor-version":[{"id":2196,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2190\/revisions\/2196"}],"wp:attachment":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}