{"id":156765,"date":"2019-09-02T11:47:51","date_gmt":"2019-09-02T03:47:51","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=156765"},"modified":"2019-08-30T09:49:13","modified_gmt":"2019-08-30T01:49:13","slug":"java-and-understand","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/java-and-understand.html","title":{"rendered":"Java\u5f02\u5e38\u673a\u5236\u6df1\u5165\u7406\u89e3"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5728\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u9519\u8bef\uff0c\u5bfc\u81f4\u7a0b\u5e8f\u51fa\u73b0\u975e\u9884\u671f\u573a\u666f\u3002\u5f02\u5e38\u5904\u7406\u53ef\u4ee5\u4fdd\u8bc1\u51fa\u73b0\u9519\u8bef\u540e\uff0c\u63a7\u5236\u63a5\u4e0b\u6765\u7684\u7a0b\u5e8f\u6d41\u7a0b\uff0c\u662f\u9009\u62e9\u5b9a\u4f4d\u9519\u8bef\u4fe1\u606f\uff0c\u8fd8\u662f\u629b\u51fa\u5f02\u5e38\u6216\u6355\u83b7\u5f02\u5e38\u3001\u8fd8\u662f\u907f\u514d\u7a0b\u5e8f\u975e\u6b63\u5e38\u9000\u51fa\uff0c\u90fd\u53d6\u51b3\u4e8e\u6211\u4eec\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
1.\u5f02\u5e38\u7684\u6982\u8ff0<\/strong><\/div>\n
1.1\u4ec0\u4e48\u662f\u5f02\u5e38\uff1f<\/strong><\/span><\/div>\n

\u5f02\u5e38\uff1a\u7a0b\u5e8f\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u53d1\u751f\u7531\u4e8e\u5916\u90e8\u95ee\u9898\u5bfc\u81f4\u7684\u7a0b\u5e8f\u5f02\u5e38\u4e8b\u4ef6\uff0c\u53d1\u751f\u7684\u5f02\u5e38\u4f1a\u4e2d\u65ad\u7a0b\u5e8f\u7684\u8fd0\u884c\u3002\uff08\u5728Java\u7b49\u9762\u5411\u5bf9\u8c61\u7684\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff09\u5f02\u5e38\u672c\u8eab\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4ea7\u751f\u5f02\u5e38\u5c31\u662f\u4ea7\u751f\u4e86\u4e00\u4e2a\u5f02\u5e38\u5bf9\u8c61\u3002\u6ce8\u610f\u5728java\u4e2d\u5f02\u5e38\u4e0d\u662f\u9519\u8bef\uff0c\u5728\u4e0b\u6587\u7684\u5f02\u5e38\u7684\u5206\u7c7b\u4e2d\u6709\u89e3\u91ca\u3002<\/p>\n

\u4e3e\u4e2a\u751f\u6d3b\u4e2d\u7684\u6817\u5b50\u6765\u8bf4\u660e\u5f02\u5e38\u5427\uff0c\u5047\u5982\u4f60\u5e73\u65f6\u662f\u5f00\u8f66\u4e0a\u73ed\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u4f60\u90fd\u80fd\u6309\u65f6\u5230\u516c\u53f8\u4e0a\u73ed\uff0c\u4f46\u662f\u4eca\u5929\u9047\u5230\u7279\u6b8a\u60c5\u51b5\uff0c\u5c31\u662f\u9047\u5230\u516c\u8def\u65bd\u5de5\u4e86\uff0c\u9047\u5230\u8fd9\u79cd\u60c5\u51b5\uff0c\u5982\u679c\u4f60\u6ca1\u6709\u505a\u4efb\u4f55\u5904\u7406\uff0c\u5c31\u6709\u53ef\u80fd\u5bfc\u81f4\u4e0a\u73ed\u8fdf\u5230\u3002\u8fd9\u79cd\u201c\u8def\u4e0a\u65bd\u5de5\u201d \u5c31\u662f\u7a0b\u5e8f\u4e2d\u7684\u5f02\u5e38\u4e86\uff0c\u4ed6\u662f\u5916\u90e8\u95ee\u9898\u5bfc\u81f4\u7684\u4e8b\u4ef6\uff0c\u5e76\u4e0d\u662f\u4f60\u672c\u8eab\u51fa\u73b0\u7684\u95ee\u9898\u3002\uff08\u9898\u5916\u8bdd\uff1a\u7136\u800c\u8001\u677f\u5e76\u4e0d\u5728\u610f\u4f60\u662f\u4ec0\u4e48\u95ee\u9898\u5bfc\u81f4\u8fdf\u5230\u7684\uff0c\u53cd\u6b63\u4f60\u5c31\u662f\u8fdf\u5230\u4e86\u3002\u3002\u3002\u3002\uff09
\n\"\"<\/p>\n

1.2 \u5982\u4f55\u5904\u7406\u5f02\u5e38\uff1f<\/strong><\/span><\/div>\n

1.2.1 \u4f20\u7edf\u7684\u5f02\u5e38\u5904\u7406<\/strong><\/p>\n

\u5047\u5982\u73b0\u5728\u8981\u6c42\u5728\u63a7\u5236\u53f0\u4e2d\uff0c\u8f93\u5165\u88ab\u9664\u6570\u548c\u9664\u6570\uff0c\u6c42\u5546\u3002<\/p>\n

\u4f20\u7edf\u505a\u6cd5\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n

\r\npublic static void main(String[] args) {\r\n        System.out.println(\"\u8bf7\u8f93\u5165\u4e00\u4e2a\u88ab\u9664\u6570:\");\r\n        Scanner sc = new Scanner(System.in);\r\n        if(sc.hasNextInt()) {            \r\n            int num1 = sc.nextInt();\r\n            System.out.println(\"\u8bf7\u8f93\u5165\u4e00\u4e2a\u9664\u6570:\");\r\n            if(sc.hasNextInt()) {                \r\n                int num2 = sc.nextInt();\r\n                if(0 == num2) {\r\n                    System.out.println(\"\u9664\u6570\u4e0d\u80fd\u4e3a0\uff01\");\r\n                }else {\r\n                    int r = num1 \/ num2;\r\n                    System.out.println(\"r = \" + r);\r\n                }\r\n            }else {\r\n                System.out.println(\"\u9664\u6570\u8f93\u5165\u4e0d\u5408\u6cd5!\");\r\n            }\r\n        }else {\r\n            \/\/\u5728\u63a7\u5236\u53f0\u6709\u53ef\u80fd\u8f93\u5165\u5b57\u7b26\u4e32\r\n            System.out.println(\"\u88ab\u9664\u6570\u8f93\u5165\u4e0d\u5408\u6cd5!\");\r\n        }\r\n\r\n    }\r\n<\/pre>\n

\u4ece\u4e0a\u9762\u8fd9\u4e2a\u4f8b\u5b50\u53ef\u4ee5\u53ef\u4ee5\u770b\u51fa\uff0c\u8fd9\u4e48\u7b80\u5355\u7684\u4e1a\u52a1\u9700\u6c42\uff0c\u4ee3\u7801\u4e5f\u8981\u5199\u5f97\u90a3\u4e48\u957f\uff0c\u56e0\u4e3a\u8981\u8003\u8651\u7684\u95ee\u9898\u6709\u5f88\u591a\uff0c\u8fd9\u6837\u5199\u4ee3\u7801\u4f1a\u89c9\u5f97\u5f88\u7d2f\uff0c\u800c\u4e14\u51fa\u73b0\u4e86\u5f02\u5e38\uff0c\u7a0b\u5e8f\u4f1a\u4e2d\u65ad\uff0c\u4e0d\u4f1a\u6267\u884c\u540e\u9762\u7684\u4ee3\u7801\u3002\u56e0\u6b64\uff0cJava\u7f16\u7a0b\u8bed\u8a00\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\u4e3a\u7a0b\u5e8f\u63d0\u4f9b\u5f02\u5e38\u5904\u7406\u7684\u80fd\u529b\u3002<\/p>\n

1.2.2 java\u7684\u5f02\u5e38\u5904\u7406<\/strong><\/p>\n

\u5728Java\u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u7684\u8fc7\u7a0b\uff1a
\n\"\"
\n\u8fd9\u79cd\u5904\u7406\u8fc7\u7a0b\u5c31\u50cf\u4f60\u4e0a\u73ed\u9047\u5230\u516c\u8def\u65bd\u5de5\uff0c\u4f60\u505a\u51fa\u4e86\u5904\u7406\u2014\u2014\u7ed5\u8def\u884c\u8d70\uff0c\u907f\u5f00\u65bd\u5de5\u8def\u6bb5\uff0c\u8ba9\u4f60\u6309\u65f6\u5230\u8fbe\u516c\u53f8\uff01<\/p>\n

2.\u5f02\u5e38\u7684\u5206\u7c7b<\/strong><\/div>\n

\"\"
\n\u5728 Java \u4e2d\uff0c\u6240\u6709\u7684\u5f02\u5e38\u90fd\u6709\u4e00\u4e2a\u5171\u540c\u7684\u7956\u5148 Throwable\uff08\u53ef\u629b\u51fa\uff09\u3002Throwable \u6307\u5b9a\u4ee3\u7801\u4e2d\u53ef\u7528\u5f02\u5e38\u4f20\u64ad\u673a\u5236\u901a\u8fc7 Java \u5e94\u7528\u7a0b\u5e8f\u4f20\u8f93\u7684\u4efb\u4f55\u95ee\u9898\u7684\u5171\u6027\u3002<\/p>\n

Throwable\uff1a \u6709\u4e24\u4e2a\u91cd\u8981\u7684\u5b50\u7c7b\uff1aException\uff08\u5f02\u5e38\uff09\u548c Error\uff08\u9519\u8bef\uff09\uff0c\u4e8c\u8005\u90fd\u662f Java \u5f02\u5e38\u5904\u7406\u7684\u91cd\u8981\u5b50\u7c7b\uff0c\u5404\u81ea\u90fd\u5305\u542b\u5927\u91cf\u5b50\u7c7b\u3002<\/p>\n

Error\uff08\u9519\u8bef\uff09:\u662f\u7a0b\u5e8f\u65e0\u6cd5\u5904\u7406\u7684\u9519\u8bef\uff0c\u8868\u793a\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u4e2d\u8f83\u4e25\u91cd\u95ee\u9898\u3002\u5927\u591a\u6570\u9519\u8bef\u4e0e\u4ee3\u7801\u7f16\u5199\u8005\u6267\u884c\u7684\u64cd\u4f5c\u65e0\u5173\uff0c\u800c\u8868\u793a\u4ee3\u7801\u8fd0\u884c\u65f6 JVM\uff08Java \u865a\u62df\u673a\uff09\u51fa\u73b0\u7684\u95ee\u9898\u3002\u4f8b\u5982\uff0cJava\u865a\u62df\u673a\u8fd0\u884c\u9519\u8bef\uff08Virtual MachineError\uff09\uff0c\u5f53 JVM \u4e0d\u518d\u6709\u7ee7\u7eed\u6267\u884c\u64cd\u4f5c\u6240\u9700\u7684\u5185\u5b58\u8d44\u6e90\u65f6\uff0c\u5c06\u51fa\u73b0 OutOfMemoryError\u3002\u8fd9\u4e9b\u5f02\u5e38\u53d1\u751f\u65f6\uff0cJava\u865a\u62df\u673a\uff08JVM\uff09\u4e00\u822c\u4f1a\u9009\u62e9\u7ebf\u7a0b\u7ec8\u6b62\u3002<\/p>\n

\u8fd9\u4e9b\u9519\u8bef\u8868\u793a\u6545\u969c\u53d1\u751f\u4e8e\u865a\u62df\u673a\u81ea\u8eab\u3001\u6216\u8005\u53d1\u751f\u5728\u865a\u62df\u673a\u8bd5\u56fe\u6267\u884c\u5e94\u7528\u65f6\uff0c\u5982Java\u865a\u62df\u673a\u8fd0\u884c\u9519\u8bef\uff08Virtual MachineError\uff09\u3001\u7c7b\u5b9a\u4e49\u9519\u8bef\uff08NoClassDefFoundError\uff09\u7b49\u3002\u8fd9\u4e9b\u9519\u8bef\u662f\u4e0d\u53ef\u67e5\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u5728\u5e94\u7528\u7a0b\u5e8f\u7684\u63a7\u5236\u548c\u5904\u7406\u80fd\u529b\u4e4b \u5916\uff0c\u800c\u4e14\u7edd\u5927\u591a\u6570\u662f\u7a0b\u5e8f\u8fd0\u884c\u65f6\u4e0d\u5141\u8bb8\u51fa\u73b0\u7684\u72b6\u51b5\u3002\u5bf9\u4e8e\u8bbe\u8ba1\u5408\u7406\u7684\u5e94\u7528\u7a0b\u5e8f\u6765\u8bf4\uff0c\u5373\u4f7f\u786e\u5b9e\u53d1\u751f\u4e86\u9519\u8bef\uff0c\u672c\u8d28\u4e0a\u4e5f\u4e0d\u5e94\u8be5\u8bd5\u56fe\u53bb\u5904\u7406\u5b83\u6240\u5f15\u8d77\u7684\u5f02\u5e38\u72b6\u51b5\u3002\u5728 Java\u4e2d\uff0c\u9519\u8bef\u901a\u8fc7Error\u7684\u5b50\u7c7b\u63cf\u8ff0\u3002<\/p>\n

Exception\uff08\u5f02\u5e38\uff09:\u662f\u7a0b\u5e8f\u672c\u8eab\u53ef\u4ee5\u5904\u7406\u7684\u5f02\u5e38\u3002Exception \u7c7b\u6709\u4e00\u4e2a\u91cd\u8981\u7684\u5b50\u7c7b RuntimeException\u3002RuntimeException \u7c7b\u53ca\u5176\u5b50\u7c7b\u8868\u793a\u201cJVM \u5e38\u7528\u64cd\u4f5c\u201d\u5f15\u53d1\u7684\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u82e5\u8bd5\u56fe\u4f7f\u7528\u7a7a\u503c\u5bf9\u8c61\u5f15\u7528\u3001\u9664\u6570\u4e3a\u96f6\u6216\u6570\u7ec4\u8d8a\u754c\uff0c\u5219\u5206\u522b\u5f15\u53d1\u8fd0\u884c\u65f6\u5f02\u5e38\uff08NullPointerException\u3001ArithmeticException\uff09\u548c ArrayIndexOutOfBoundException\u3002\u6ce8\u610f\uff1a\u5f02\u5e38\u548c\u9519\u8bef\u7684\u533a\u522b\uff1a\u5f02\u5e38\u80fd\u88ab\u7a0b\u5e8f\u672c\u8eab\u53ef\u4ee5\u5904\u7406\uff0c\u9519\u8bef\u662f\u65e0\u6cd5\u5904\u7406\u3002\u901a\u5e38\uff0cJava\u7684\u5f02\u5e38(\u5305\u62ecException\u548cError)\u5206\u4e3a\u53ef\u67e5\u7684\u5f02\u5e38\uff08checked exceptions\uff09\u548c\u4e0d\u53ef\u67e5\u7684\u5f02\u5e38\uff08unchecked exceptions\uff09\u3002<\/p>\n

\u53ef\u67e5\u5f02\u5e38\uff08\u7f16\u8bd1\u5668\u8981\u6c42\u5fc5\u987b\u5904\u7f6e\u7684\u5f02\u5e38\uff09\uff1a\u6b63\u786e\u7684\u7a0b\u5e8f\u5728\u8fd0\u884c\u4e2d\uff0c\u5f88\u5bb9\u6613\u51fa\u73b0\u7684\u3001\u60c5\u7406\u53ef\u5bb9\u7684\u5f02\u5e38\u72b6\u51b5\u3002\u53ef\u67e5\u5f02\u5e38\u867d\u7136\u662f\u5f02\u5e38\u72b6\u51b5\uff0c\u4f46\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u5b83\u7684\u53d1\u751f\u662f\u53ef\u4ee5\u9884\u8ba1\u7684\uff0c\u800c\u4e14\u4e00\u65e6\u53d1\u751f\u8fd9\u79cd\u5f02\u5e38\u72b6\u51b5\uff0c\u5c31\u5fc5\u987b\u91c7\u53d6\u67d0\u79cd\u65b9\u5f0f\u8fdb\u884c\u5904\u7406\u3002<\/p>\n

\u9664\u4e86RuntimeException\u53ca\u5176\u5b50\u7c7b\u4ee5\u5916\uff0c\u5176\u4ed6\u7684Exception\u7c7b\u53ca\u5176\u5b50\u7c7b\u90fd\u5c5e\u4e8e\u53ef\u67e5\u5f02\u5e38\u3002\u8fd9\u79cd\u5f02\u5e38\u7684\u7279\u70b9\u662fJava\u7f16\u8bd1\u5668\u4f1a\u68c0\u67e5\u5b83\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f53\u7a0b\u5e8f\u4e2d\u53ef\u80fd\u51fa\u73b0\u8fd9\u7c7b\u5f02\u5e38\uff0c\u8981\u4e48\u7528try-catch\u8bed\u53e5\u6355\u83b7\u5b83\uff0c\u8981\u4e48\u7528throws\u5b50\u53e5\u58f0\u660e\u629b\u51fa\u5b83\uff0c\u5426\u5219\u7f16\u8bd1\u4e0d\u4f1a\u901a\u8fc7\u3002<\/p>\n

\u4e0d\u53ef\u67e5\u5f02\u5e38(\u7f16\u8bd1\u5668\u4e0d\u8981\u6c42\u5f3a\u5236\u5904\u7f6e\u7684\u5f02\u5e38):\u5305\u62ec\u8fd0\u884c\u65f6\u5f02\u5e38\uff08RuntimeException\u4e0e\u5176\u5b50\u7c7b\uff09\u548c\u9519\u8bef\uff08Error\uff09\u3002<\/p>\n

Exception \u8fd9\u79cd\u5f02\u5e38\u5206\u4e24\u5927\u7c7b\u8fd0\u884c\u65f6\u5f02\u5e38\u548c\u975e\u8fd0\u884c\u65f6\u5f02\u5e38(\u7f16\u8bd1\u5f02\u5e38)\u3002\u7a0b\u5e8f\u4e2d\u5e94\u5f53\u5c3d\u53ef\u80fd\u53bb\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002<\/p>\n

\u8fd0\u884c\u65f6\u5f02\u5e38\uff1a\u90fd\u662fRuntimeException\u7c7b\u53ca\u5176\u5b50\u7c7b\u5f02\u5e38\uff0c\u5982NullPointerException(\u7a7a\u6307\u9488\u5f02\u5e38)\u3001IndexOutOfBoundsException(\u4e0b\u6807\u8d8a\u754c\u5f02\u5e38)\u7b49\uff0c\u8fd9\u4e9b\u5f02\u5e38\u662f\u4e0d\u68c0\u67e5\u5f02\u5e38\uff0c\u7a0b\u5e8f\u4e2d\u53ef\u4ee5\u9009\u62e9\u6355\u83b7\u5904\u7406\uff0c\u4e5f\u53ef\u4ee5\u4e0d\u5904\u7406\u3002\u8fd9\u4e9b\u5f02\u5e38\u4e00\u822c\u662f\u7531\u7a0b\u5e8f\u903b\u8f91\u9519\u8bef\u5f15\u8d77\u7684\uff0c\u7a0b\u5e8f\u5e94\u8be5\u4ece\u903b\u8f91\u89d2\u5ea6\u5c3d\u53ef\u80fd\u907f\u514d\u8fd9\u7c7b\u5f02\u5e38\u7684\u53d1\u751f\u3002\u8fd0\u884c\u65f6\u5f02\u5e38\u7684\u7279\u70b9\u662fJava\u7f16\u8bd1\u5668\u4e0d\u4f1a\u68c0\u67e5\u5b83\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f53\u7a0b\u5e8f\u4e2d\u53ef\u80fd\u51fa\u73b0\u8fd9\u7c7b\u5f02\u5e38\uff0c\u5373\u4f7f\u6ca1\u6709\u7528try-catch\u8bed\u53e5\u6355\u83b7\u5b83\uff0c\u4e5f\u6ca1\u6709\u7528throws\u5b50\u53e5\u58f0\u660e\u629b\u51fa\u5b83\uff0c\u4e5f\u4f1a\u7f16\u8bd1\u901a\u8fc7\u3002<\/p>\n

\u975e\u8fd0\u884c\u65f6\u5f02\u5e38 \uff08\u7f16\u8bd1\u5f02\u5e38\uff09\uff1a\u662fRuntimeException\u4ee5\u5916\u7684\u5f02\u5e38\uff0c\u7c7b\u578b\u4e0a\u90fd\u5c5e\u4e8eException\u7c7b\u53ca\u5176\u5b50\u7c7b\u3002\u4ece\u7a0b\u5e8f\u8bed\u6cd5\u89d2\u5ea6\u8bb2\u662f\u5fc5\u987b\u8fdb\u884c\u5904\u7406\u7684\u5f02\u5e38\uff0c\u5982\u679c\u4e0d\u5904\u7406\uff0c\u7a0b\u5e8f\u5c31\u4e0d\u80fd\u7f16\u8bd1\u901a\u8fc7\u3002\u5982IOException\u3001SQLException\u7b49\u4ee5\u53ca\u7528\u6237\u81ea\u5b9a\u4e49\u7684Exception\u5f02\u5e38\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u4e0d\u81ea\u5b9a\u4e49\u68c0\u67e5\u5f02\u5e38\u3002<\/p>\n

3.\u5f02\u5e38\u5904\u7406\u673a\u5236<\/strong><\/div>\n

\u901a\u8fc7\u4e0a\u9762\u5bf9\u5f02\u5e38\u7684\u89e3\u91ca\uff0c\u73b0\u5728\u5e94\u8be5\u5bf9\u5f02\u5e38\u6709\u4e00\u5b9a\u7684\u4e86\u89e3\u3002\u4e0b\u9762\u6765\u8bf4\u660e\u5728java\u4e2d\u662f\u5982\u4f55\u5904\u7406\u5f02\u5e38\u7684\u3002
\n\u2022\u5728java\u4e2d\u7528\u5bf9\u8c61\u6765\u8868\u793a\u5f02\u5e38\u7684\u3002
\n\u2022java\u662f\u901a\u8fc7try-catch\u3001try-catch-finally\u3001try-catch-catch...\u8bed\u53e5\u6765\u5904\u7406\u5f02\u5e38\u7684\u3002<\/p>\n

3.1 try-catch<\/strong><\/span><\/div>\n

3.1.1 try-catch\u7684\u4f7f\u7528<\/strong><\/p>\n

\r\ntry{} \u4ee3\u7801\u5757\u7528\u4e8e\u6267\u884c\u53ef\u80fd\u5b58\u5728\u5f02\u5e38\u7684\u4ee3\u7801\uff0ccatch(\u5f02\u5e38\u7c7b\u578b  \u5f02\u5e38\u5bf9\u8c61\u7684\u540d\u79f0){}\u4ee3\u7801\u5757\u7528\u4e8e\u6355\u83b7\u5e76\u5904\u7406\u5f02\u5e38\u3002\r\n\r\ntry{\r\n    \/\/\u6709\u53ef\u80fd\u51fa\u73b0\u5f02\u5e38\u7684\u4ee3\u7801\u6bb51\r\n    \/\/\u6709\u53ef\u80fd\u51fa\u73b0\u5f02\u5e38\u7684\u4ee3\u7801\u6bb52\r\n}catch(\u5f02\u5e38\u7c7b\u578b e){\r\n    \/\/\u5904\u7406\u5f02\u5e38\u7684\u4ee3\u7801\u6bb53\r\n}\r\n\/\/\u4ee3\u7801\u6bb54\r\n<\/pre>\n

\u5904\u74061.2.1\u7684\u4e24\u6570\u76f8\u9664\u95ee\u9898\u7684demo\uff1a<\/p>\n

\r\npublic static void main(String[] args) {\r\n        Scanner sc = new Scanner(System.in);\r\n        try{\r\n            System.out.println(\"\u8bf7\u8f93\u5165\u4e00\u4e2a\u88ab\u9664\u6570:\");\r\n            int num1=sc.nextInt();\r\n            System.out.println(\"\u8bf7\u8f93\u5165\u4e00\u4e2a\u9664\u6570:\");\r\n            int num2=sc.nextInt();\r\n            int result=num1\/num2;\r\n        }catch(Exception e){\r\n            System.out.println(\"\u5728\u6b64\u5904\u5904\u7406\u5f02\u5e38\uff01\");\r\n        }\r\n        System.out.println(\"\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u675f\");\r\n\r\n}\r\n<\/pre>\n

3.1.2 try-catch\u7684\u6267\u884c\u987a\u5e8f<\/strong><\/p>\n

\u7b2c\u4e00\u79cd\uff1a\u6ca1\u6709\u9047\u5230\u5f02\u5e38\uff0c\u5373\u6b63\u5e38\u6267\u884c
\n\"\"
\n\u7b2c\u4e8c\u79cd\uff1a\u5339\u914d\u5230\u5f02\u5e38
\n\"\"
\n\u5728try{}\u4e2d\u7684\u4ee3\u7801\u9047\u5230\u5f02\u5e38\u65f6\uff0c\u4f1a\u4e0ecatch\uff08\uff09\u4e2d\u62ec\u53f7\u91cc\u7684\u5f02\u5e38\u8fdb\u884c\u6bd4\u5bf9\uff0c\u5982\u679c\u9047\u5230\u7684\u5f02\u5e38\u65f6\u5c5e\u4e8ecatch\u7684\u5f02\u5e38\u5c31\u4f1a\u6267\u884ccatch\u5757\u4e2d\u7684\u4ee3\u7801\uff0c\u8ba9\u540e\u6267\u884ctry-catch\u5757\u540e\u9762\u7684\u4ee3\u7801<\/p>\n

\u7b2c\u4e09\u79cd\uff1a\u5339\u914d\u4e0d\u5230\u5f02\u5e38
\n\"\"<\/p>\n

3.2try-catch-finally<\/strong><\/span><\/div>\n

try{} \u4ee3\u7801\u5757\u7528\u4e8e\u6267\u884c\u53ef\u80fd\u5b58\u5728\u5f02\u5e38\u7684\u4ee3\u7801\uff0ccatch{}\u4ee3\u7801\u5757\u7528\u4e8e\u6355\u83b7\u5e76\u5904\u7406\u5f02\u5e38\u3002<\/p>\n

finally{} \u4ee3\u7801\u5757\u7528\u4e8e\u56de\u6536\u8d44\u6e90(\u5173\u95ed\u6587\u4ef6\u3001\u5173\u95ed\u6570\u636e\u5e93\u3001\u5173\u95ed\u7ba1\u9053)\u7684\u4ee3\u7801\u3002finally\u4ee3\u7801\u5757\u4e0d\u7ba1\u662f\u5426\u51fa\u73b0\u5f02\u5e38\uff0c\u90fd\u5fc5\u987b\ufffd\ufffd\ufffd\u884c\uff08 finally\u5757\u552f\u4e00\u4e0d\u6267\u884c\u7684\u60c5\u51b5System.exit(0) jvm\u6b63\u5e38\u9000\u51fa\u3002\uff09<\/p>\n

3.1.1 try-catch-finally\u7684\u6267\u884c\u987a\u5e8f<\/p>\n

\u7b2c\u4e00\u79cd\uff1acatch\u5757\u6ca1\u6709return\u8bed\u53e5<\/p>\n

\uff081\uff09\u6ca1\u6709\u9047\u5230\u5f02\u5e38\uff1a<\/p>\n

try\u5757\u5185\u7684\u4ee3\u7801\u2014\u2014>finally\u5757\u5185\u7684\u4ee3\u7801\u2014\u2014>finally\u5757\u540e\u7684\u4ee3\u7801<\/p>\n

\uff082\uff09\u9047\u5230\u5f02\u5e38\u5e76\u5339\u914d\u5230\u5f02\u5e38\uff1a<\/p>\n

try\u5757\u5185\u7684\u4ee3\u7801\u2014\u2014>catch\u5757\u5185\u7684\u4ee3\u7801\u2014\u2014>finally\u5757\u5185\u7684\u4ee3\u7801\u2014\u2014>finally\u5757\u540e\u7684\u4ee3\u7801<\/p>\n

\r\npublic class Test01 {\r\n    public static void main(String[] args) {\r\n        try {\r\n            int a=1\/0;\r\n            System.out.println(\"try\");\r\n        } catch (Exception e) {\r\n            System.out.println(\"catch\");\r\n        }finally {\r\n            System.out.println(\"finally\");\r\n        }\r\n        System.out.println(\"\u7a0b\u5e8f\u6b63\u5e38\u8fd0\u884c\u7ed3\u675f\");\r\n    }\r\n}\r\n<\/pre>\n

\u7ed3\u679c\uff1a<\/p>\n

1 catch
\n2 finally
\n3 \u7a0b\u5e8f\u6b63\u5e38\u8fd0\u884c\u7ed3\u675f<\/p>\n

\uff083\uff09\u9047\u5230\u5f02\u5e38\u5374\u6ca1\u6709\u5339\u914d\u5230\u5f02\u5e38\uff1a<\/p>\n

try\u5757\u5185\u7684\u4ee3\u7801\u2014\u2014>finally\u5757\u5185\u7684\u4ee3\u7801\u2014\u2014>\u7a0b\u5e8f\u4e2d\u65ad\u8fd0\u884c<\/p>\n

\r\npublic static void main(String[] args) {\r\n    try {\r\n        int a=1\/0;\/\/\u4f1a\u629b\u51faArithmeticException\r\n        System.out.println(\"try\");\r\n    } catch (NullPointerException e) {\r\n        System.out.println(\"catch\");\r\n    }finally {\r\n        System.out.println(\"finally\");\r\n    }\r\n    System.out.println(\"\u7a0b\u5e8f\u6b63\u5e38\u8fd0\u884c\u7ed3\u675f\");\r\n}\r\n<\/pre>\n

\u7ed3\u679c\uff1a<\/p>\n

\r\n1 Exception in thread \"main\" finally\r\n2 java.lang.ArithmeticException: \/ by zero\r\n3     at Test1.Test01.main(Test01.java:8)\r\n<\/pre>\n

\u7b2c\u4e8c\u79cd\uff1acatch\u5757\u6709return\u8bed\u53e5<\/p>\n

\uff081\uff09\u6ca1\u6709\u5f02\u5e38<\/p>\n

try\u5757\u5185\u7684\u4ee3\u7801\u2014\u2014>finally\u5757\u5185\u7684\u4ee3\u7801\u2014\u2014>try\u5757\u5185\u7684return\u8bed\u53e5<\/p>\n

\r\npublic class Test01 {\r\n    public static int test() {\r\n        try {\r\n            int a=2*2;\r\n            System.out.println(\"try\");\r\n            return a;\r\n        } catch (Exception e) {\r\n            System.out.println(\"catch\");\r\n            return 0;\r\n        }finally {\r\n            System.out.println(\"finally\");\r\n        }\r\n    }\r\n    public static void main(String[] args) {\r\n        System.out.println(test());\r\n        System.out.println(\"\u7a0b\u5e8f\u6b63\u5e38\u8fd0\u884c\u7ed3\u675f\");\r\n    }\r\n}\r\n<\/pre>\n

\u7ed3\u679c\uff1a<\/p>\n

\r\n1 try\r\n2 finally\r\n3 4\r\n4 \u7a0b\u5e8f\u6b63\u5e38\u8fd0\u884c\u7ed3\u675f\r\n<\/pre>\n

\uff082\uff09\u9047\u5230\u5f02\u5e38\u5e76\u5339\u914d\u5230\u5f02\u5e38\uff1a<\/p>\n

\u6267\u884c\u987a\u5e8f\u5982\u56fe\uff1a
\n\"\"<\/p>\n

\r\npublic class Test01 {\r\n    public static int test() {\r\n        try {\r\n            int a=2\/0;\r\n            System.out.println(\"try\");\r\n            return a;\r\n        } catch (Exception e) {\r\n            System.out.println(\"catch\");\r\n            return 0;\r\n        }finally {\r\n            System.out.println(\"finally\");\r\n        }\r\n    }\r\n    public static void main(String[] args) {\r\n        System.out.println(test());\r\n        System.out.println(\"\u7a0b\u5e8f\u6b63\u5e38\u8fd0\u884c\u7ed3\u675f\");\r\n    }\r\n}\r\n<\/pre>\n

\u7ed3\u679c\uff1a<\/p>\n

\r\n1 catch\r\n2 finally\r\n3 0\r\n4 \u7a0b\u5e8f\u6b63\u5e38\u8fd0\u884c\u7ed3\u675f\r\n<\/pre>\n

\uff083\uff09\u9047\u5230\u5f02\u5e38\u5374\u6ca1\u6709\u5339\u914d\u5230\u5f02\u5e38\uff1a<\/p>\n

try\u5757\u5185\u7684\u4ee3\u7801\u2014\u2014>finally\u5757\u5185\u7684\u4ee3\u7801\u2014\u2014>\u7a0b\u5e8f\u4e2d\u65ad\u8fd0\u884c<\/p>\n

\r\npublic class Test01 {\r\n    public static int test() {\r\n        try {\r\n            int a=2\/0;\r\n            System.out.println(\"try\");\r\n            return a;\r\n        } catch (NullPointerException e) {\r\n            System.out.println(\"catch\");\r\n            return 0;\r\n        }finally {\r\n            System.out.println(\"finally\");\r\n        }\r\n    }\r\n    public static void main(String[] args) {\r\n        System.out.println(test());\r\n        System.out.println(\"\u7a0b\u5e8f\u6b63\u5e38\u8fd0\u884c\u7ed3\u675f\");\r\n    }\r\n}\r\n<\/pre>\n

\u7ed3\u679c\uff1a<\/p>\n

\r\nfinally\r\nException in thread \"main\" java.lang.ArithmeticException: \/ by zero\r\n    at Test1.Test01.test(Test01.java:8)\r\n    at Test1.Test01.main(Test01.java:19)\r\n<\/pre>\n
4. \u58f0\u660e\u5f02\u5e38<\/strong><\/div>\n
4.1 throws<\/strong><\/span><\/div>\n

\u5f53\u5f00\u53d1\u8005\u5728\u5b9a\u4e49\u65b9\u6cd5\u65f6\uff0c\u4e8b\u5148\u77e5\u9053\u65b9\u6cd5\u5728\u8c03\u7528\u65f6\u4f1a\u51fa\u73b0\u5f02\u5e38\uff0c\u4f46\u4e0d\u77e5\u9053\u8be5\u5982\u4f55\u5904\u7406\uff0c\u6b64\u65f6\u53ef\u4ee5\u5728\u8be5\u65b9\u6cd5\u4e0a\u58f0\u660e\u5f02\u5e38\u3002\u8868\u793a\u8be5\u65b9\u6cd5\u5728\u8c03\u7528\u8fc7\u7a0b\u4e2d\u4f1a\u51fa\u73b0\u5f02\u5e38\uff0c\u8bf7\u8c03\u7528\u8005\u81ea\u884c\u5904\u7406\u3002<\/p>\n

\u5728java\u4e2d\u4f7f\u7528throws \u58f0\u660e\u5f02\u5e38\u3002\u4e00\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u58f0\u660e\u591a\u4e2a\u5f02\u5e38\uff0c\u7528,\u53f7\u5206\u5272\uff0c\u5199\u6cd5\u5982\u4e0b\uff1a<\/p>\n

\r\n1 public void test2()throws IOException,RuntimeException{\r\n2         \/\/\u6709\u5f02\u5e38\u51fa\u5f97\u4ee3\u7801\uff0c\u5728\u6b64\u5904\u6ca1\u6709\u5904\u7406\r\n3     }\r\n<\/pre>\n
4.2 \u58f0\u660e\u5f02\u5e38\u4e0e\u65b9\u6cd5\u91cd\u8f7d\u7684\u5173\u7cfb<\/strong><\/span><\/div>\n

\u58f0\u660e\u5f02\u5e38\u548c\u65b9\u6cd5\u91cd\u8f7d\u6ca1\u6709\u4efb\u4f55\u5173\u7cfb\u3002<\/p>\n

4.3 \u58f0\u660e\u5f02\u5e38\u4e0e\u65b9\u6cd5\u91cd\u5199\u7684\u5173\u7cfb<\/strong><\/span><\/div>\n

\u2022\u5982\u679c\u7236\u7c7b\u65b9\u6cd5\u58f0\u660e\u4e86\u5f02\u5e38(\u68c0\u67e5\u65f6\u6216\u8fd0\u884c\u65f6)\uff0c\u5b50\u7c7b\u65b9\u6cd5\u53ef\u4ee5\u5b8c\u5168\u9075\u5faa\u7236\u7c7b\u5f02\u5e38\uff0c\u4e5f\u53ef\u4ee5\u4e0d\u58f0\u660e\u5f02\u5e38\u3002
\n\u2022\u5982\u679c\u7236\u7c7b\u65b9\u6cd5\u6ca1\u6709\u58f0\u660e\u5f02\u5e38\uff0c\u5b50\u7c7b\u53ef\u4ee5\u4e0d\u58f0\u660e\u5f02\u5e38\uff0c\u4e5f\u53ef\u4ee5\u58f0\u660eRuntimeException\uff0c\u4f46\u4e0d\u80fd\u58f0\u660eException\u3002
\n\u2022\u5982\u679c\u7236\u7c7b\u58f0\u660e\u4e86\u8fd0\u884c\u65f6\u5f02\u5e38\uff0c\u5b50\u7c7b\u53ef\u4ee5\u5b8c\u5168\u9075\u5faa\u7236\u7c7b\u5f02\u5e38\uff0c\u4e5f\u53ef\u4ee5\u4e0d\u58f0\u660e\u5f02\u5e38\u3002<\/p>\n

5.\u629b\u51fa\u5f02\u5e38<\/strong><\/div>\n

\u5f53\u7cfb\u7edf\u5f02\u5e38\u6ee1\u8db3\u4e0d\u4e86\u5f00\u53d1\u9700\u8981\u65f6\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u81ea\u884c\u6839\u636e\u9700\u8981\u81ea\u884c\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n

throw \u7528\u4e8e\u624b\u52a8\u629b\u51fa\u5f02\u5e38\u3002
\n\"\"
\n\u5982\u679c\u4e00\u76f4\u90fd\u6ca1\u6709\u5904\u7406\uff08\u5373\u6ca1\u6709\u7528try-catch\u7b49\u8bed\u53e5\uff09\u5f02\u5e38\u4f1a\u628a\u5f02\u5e38\u629b\u7ed9\u8c03\u7528\u8005\uff0c\u4e00\u76f4\u629b\u5230main\u51fd\u6570\u5904\uff0c\u5982\u679c\u5728main\u51fd\u6570\u4e2d\u4e5f\u6ca1\u6709\u5904\u7406\u7ee7\u7eed\u5728main\u51fd\u6570\u540e\u629b\u51fa\u5f02\u5e38\uff0c\u8fd9\u65f6\u5019\u4f1a\u629b\u7ed9jvm\u5904\u7406\u3002\u5982\u4e0b\u6817\u5b50\uff1a<\/p>\n

\r\npublic class Test01 {\r\n    public static void test1()throws IOException,RuntimeException{\r\n        \/\/\u6709\u5f02\u5e38\u629b\u51fa\u5f97\u4ee3\u7801\uff0c\u5728\u6b64\u5904\u6ca1\u6709\u5904\u7406\uff0c\u4f8b\u5982\uff1athrow new Exception\uff08\u201c\u5f02\u5e38\u4fe1\u606f\u201d\uff09\uff1b\r\n    }\r\n    public static void test2()throws IOException,RuntimeException{\r\n        test1();\/\/\u8c03\u7528\u6709\u629b\u51fa\u5f02\u5e38\u7684\u65b9\u6cd5\uff0c\u5728\u6b64\u6ca1\u6709\u5904\u7406\r\n    }\r\n    public static void main(String[] args)throws IOException,RuntimeException {\r\n        test2();\/\/main\u8c03\u7528\u6709\u629b\u51fa\u5f02\u5e38\u7684\u65b9\u6cd5\uff0c\u5728\u6b64\u6ca1\u6709\u5904\u7406\r\n    }\r\n}\r\n<\/pre>\n

\u6ce8\u610f:<\/p>\n

\u5f00\u53d1\u8005\u6839\u636e\u81ea\u8eab\u9700\u8981\u53ef\u4ee5\u9009\u62e9\u629b\u51fa\u68c0\u67e5\u65f6\u5f02\u5e38\u548c\u8fd0\u884c\u65f6\u5f02\u5e38<\/p>\n

6. \u81ea\u5b9a\u4e49\u5f02\u5e38<\/strong><\/div>\n

\u5f53JDK \u4e2d\u7684\u5f02\u5e38\u7c7b\u578b\u4e0d\u80fd\u6ee1\u8db3\u7a0b\u5e8f\u7684\u9700\u8981\u65f6\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u3002<\/p>\n

\u81ea\u5b9a\u4e49\u5f02\u5e38\u6b65\u9aa4:
\n\u2022[1] \u786e\u5b9a\u5f02\u5e38\u7c7b\u578b.\u7ee7\u627fExcepion \u6216\u8005RuntimeException
\n\u2022[2] \u7f16\u5199\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\uff0c\u5e76\u5b9e\u73b0\u6784\u9020\u65b9\u6cd5
\n\u2022[3] \u5728\u65b9\u6cd5\u9700\u8981\u7684\u5730\u65b9\u624b\u52a8\u58f0\u660e\u5e76\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n

\r\npublic class myException extends Exception {\r\n\r\n    public myException() {\r\n        super();\r\n    }\r\n\r\n    public myException(String message) {\r\n        super(message);\r\n    }\r\n    \r\n    \/\/\u81ea\u5b9a\u4e49\u5f02\u5e38\u4e2d\u7684\u65b9\u6cd5\uff0c\u4ee5\u7b26\u5408\u81ea\u5df1\u7684\u9700\u6c42\r\n    public void showInfo() {\r\n        System.out.println(super.getMessage()+\"@Line:\");\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u5f02\u5e38\uff1a\u7a0b\u5e8f\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u53d1\u751f\u7531\u4e8e\u5916\u90e8\u95ee\u9898\u5bfc\u81f4\u7684\u7a0b\u5e8f\u5f02\u5e38\u4e8b\u4ef6\uff0c\u53d1\u751f\u7684\u5f02\u5e38\u4f1a\u4e2d\u65ad\u7a0b\u5e8f\u7684\u8fd0\u884c\u3002\uff08\u5728Java\u7b49\u9762\u5411\u5bf9\u8c61\u7684 […]<\/p>\n","protected":false},"author":63,"featured_media":156773,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-156765","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/156765","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=156765"}],"version-history":[{"count":12,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/156765\/revisions"}],"predecessor-version":[{"id":157585,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/156765\/revisions\/157585"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/156773"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=156765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=156765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=156765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}