{"id":88670,"date":"2026-08-01T15:24:46","date_gmt":"2026-08-01T07:24:46","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/88670.html"},"modified":"2026-08-01T15:24:46","modified_gmt":"2026-08-01T07:24:46","slug":"java-%e5%bc%82%e5%b8%b8%e2%91%a0","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/88670.html","title":{"rendered":"Java \u5f02\u5e38\u2460"},"content":{"rendered":"<\/p>\n<h4>\u76ee\u5f55<\/h4>\n<ul>\n<li>\u6587\u7ae0\u6458\u8981<\/li>\n<li>1. \u4ec0\u4e48\u662f\u5f02\u5e38<\/li>\n<li>2. \u5f02\u5e38&#xff08;Exception&#xff09;<\/li>\n<li>3. \u9519\u8bef&#xff08;Error&#xff09;<\/li>\n<li>3. try-catch \u5904\u7406\u5f02\u5e38<\/li>\n<\/ul>\n<h2>\u6587\u7ae0\u6458\u8981<\/h2>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86Java\u4e2d\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\u3002 \u4e3b\u8981\u5185\u5bb9\u5305\u62ec&#xff1a; 1.\u5f02\u5e38\u7684\u6982\u5ff5&#xff0c;\u5373\u5728\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u7684\u4e0d\u6b63\u5e38\u60c5\u51b5&#xff0c;\u5982\u7b97\u672f\u5f02\u5e38\u3001\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38\u7b49\u5e38\u89c1\u7c7b\u578b&#xff1b; 2.\u5f02\u5e38\u5206\u7c7b&#xff0c;\u5206\u4e3a\u7f16\u8bd1\u65f6\u5f02\u5e38&#xff08;\u5982IOException&#xff09;\u548c\u8fd0\u884c\u65f6\u5f02\u5e38&#xff08;\u5982NullPointerException&#xff09;&#xff1b; 3.\u9519\u8bef\u548c\u5f02\u5e38\u7684\u533a\u522b&#xff1b; 4.try-catch\u5f02\u5e38\u5904\u7406\u65b9\u6cd5&#xff0c;\u901a\u8fc7\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38&#xff0c;\u5305\u62ec\u6355\u83b7\u591a\u4e2a\u5f02\u5e38\u3001\u4f7f\u7528printStackTrace()\u5b9a\u4f4d\u9519\u8bef&#xff0c;\u4ee5\u53ca\u5f02\u5e38\u53d1\u751f\u540e\u4ee3\u7801\u6267\u884c\u7684\u6d41\u7a0b\u3002\u6587\u7ae0\u5f3a\u8c03\u5f02\u5e38\u5904\u7406\u53ef\u4ee5\u4fdd\u8bc1\u7a0b\u5e8f\u5728\u51fa\u73b0\u9519\u8bef\u540e\u4ecd\u80fd\u7ee7\u7eed\u6267\u884c\u540e\u7eed\u4ee3\u7801\u3002<\/p>\n<h2>1. \u4ec0\u4e48\u662f\u5f02\u5e38<\/h2>\n<p>\u5f02\u5e38&#xff1a;\u5728\u4ee3\u7801\u6267\u884c\u8fc7\u7a0b&#xff0c;\u51fa\u73b0\u4e0d\u6b63\u5e38\u7684\u60c5\u51b5&#xff0c;\u5bfc\u81f4\u7a0b\u5e8f\u65e0\u6cd5\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n<p>\u4f8b\u5982&#xff1b; 1.1 \u7b97\u6570\u5f02\u5e38<\/p>\n<p><span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">10<\/span><span class=\"token operator\">\/<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260801072444-6a6d9f3c02bbe.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/>  2.2 \u6570\u7ec4\u8d8a\u754c\u5f02\u5e38<\/p>\n<p><span class=\"token keyword\">int<\/span><span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> arr <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token keyword\">int<\/span><span class=\"token punctuation\">[<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span>arr<span class=\"token punctuation\">[<\/span><span class=\"token number\">3<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260801072444-6a6d9f3c25fef.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/>  \u5e38\u89c1\u5f02\u5e38&#xff1a;<\/p>\n<table>\n<tr>\u5f02\u5e38\u539f\u56e0<\/tr>\n<tbody>\n<tr>\n<td>NullPointerException<\/td>\n<td>\u7a7a\u6307\u9488<\/td>\n<\/tr>\n<tr>\n<td>ArrayIndexOutOfBoundsException<\/td>\n<td>\u6570\u7ec4\u8d8a\u754c<\/td>\n<\/tr>\n<tr>\n<td>ArithmeticException<\/td>\n<td>\u7b97\u6570\u9519\u8bef<\/td>\n<\/tr>\n<tr>\n<td>ClassCastException<\/td>\n<td>\u7c7b\u578b\u8f6c\u6362\u9519\u8bef<\/td>\n<\/tr>\n<tr>\n<td>NumberFormatException<\/td>\n<td>\u6570\u5b57\u683c\u5f0f\u9519\u8bef<\/td>\n<\/tr>\n<tr>\n<td>IOException<\/td>\n<td>\u8f93\u5165\u8f93\u51fa\u5f02\u5e38<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Exception\u90fd\u662f\u5f02\u5e38<\/p>\n<h2>2. \u5f02\u5e38&#xff08;Exception&#xff09;<\/h2>\n<p>2.1 \u7f16\u8bd1\u65f6\u5f02\u5e38&#xff08;Checked Exception&#xff09;<\/p>\n<p>\u4e3b\u8981\u53d1\u751f\u5728\u7f16\u8bd1\u9636\u6bb5<\/p>\n<p>\u5e38\u89c1\u7f16\u8bd1\u65f6\u5f02\u5e38 &#xff1a;<\/p>\n<table>\n<tr>\u7f16\u8bd1\u65f6\u5f02\u5e38\u539f\u56e0<\/tr>\n<tbody>\n<tr>\n<td>IOException<\/td>\n<td>\u8f93\u5165\u8f93\u51fa\u5f02\u5e38<\/td>\n<\/tr>\n<tr>\n<td>FileNotFoundException<\/td>\n<td>\u6587\u4ef6\u4e0d\u5b58\u5728<\/td>\n<\/tr>\n<tr>\n<td>SQLException<\/td>\n<td>\u6570\u636e\u5e93\u5f02\u5e38<\/td>\n<\/tr>\n<tr>\n<td>ClassNotFoundException<\/td>\n<td>\u7c7b\u627e\u4e0d\u5230<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>2.2 \u8fd0\u884c\u65f6\u5f02\u5e38&#xff08;Runtime Exception&#xff09;<\/p>\n<p>\u7f16\u8bd1\u5668\u65e0\u6cd5\u63d0\u524d\u5224\u65ad&#xff0c;\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u624d\u53ef\u80fd\u51fa\u73b0\u3002<\/p>\n<p>\u5e38\u89c1\u8fd0\u884c\u65f6\u5f02\u5e38 &#xff1a;<\/p>\n<table>\n<tr>\u8fd0\u884c\u65f6\u5f02\u5e38\u539f\u56e0<\/tr>\n<tbody>\n<tr>\n<td>NullPointerException<\/td>\n<td>\u7a7a\u6307\u9488<\/td>\n<\/tr>\n<tr>\n<td>ArrayIndexOutOfBoundsException<\/td>\n<td>\u6570\u7ec4\u8d8a\u754c<\/td>\n<\/tr>\n<tr>\n<td>ArithmeticException<\/td>\n<td>\u6570\u5b66\u9519\u8bef<\/td>\n<\/tr>\n<tr>\n<td>ClassCastException<\/td>\n<td>\u7c7b\u578b\u8f6c\u6362\u9519\u8bef<\/td>\n<\/tr>\n<tr>\n<td>NumberFormatException<\/td>\n<td>\u6570\u5b57\u683c\u5f0f\u9519\u8bef<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>3. \u9519\u8bef&#xff08;Error&#xff09;<\/h2>\n<p>\u5728 Java \u4e2d&#xff0c;\u6240\u6709\u7684\u5f02\u5e38\u90fd\u6709\u4e00\u4e2a\u5171\u540c\u7684\u7956\u5148 Throwable&#xff08;\u53ef\u629b\u51fa&#xff09;\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 Throwable&#xff1a; \u6709\u4e24\u4e2a\u91cd\u8981\u7684\u5b50\u7c7b&#xff1a;Exception&#xff08;\u5f02\u5e38&#xff09;\u548c Error&#xff08;\u9519\u8bef&#xff09;&#xff0c;\u4e8c\u8005\u90fd\u662f Java \u5f02\u5e38\u5904\u7406\u7684\u91cd\u8981\u5b50\u7c7b&#xff0c;\u5404\u81ea\u90fd\u5305\u542b\u5927\u91cf\u5b50\u7c7b\u3002 Error&#xff08;\u9519\u8bef&#xff09;:\u662f\u7a0b\u5e8f\u65e0\u6cd5\u5904\u7406\u7684\u9519\u8bef&#xff0c;\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&#xff0c;\u800c\u8868\u793a\u4ee3\u7801\u8fd0\u884c\u65f6 JVM&#xff08;Java \u865a\u62df\u673a&#xff09;\u51fa\u73b0\u7684\u95ee\u9898\u3002\u4f8b\u5982&#xff0c;Java\u865a\u62df\u673a\u8fd0\u884c\u9519\u8bef&#xff08;Virtual MachineError&#xff09;&#xff0c;\u5f53 JVM \u4e0d\u518d\u6709\u7ee7\u7eed\u6267\u884c\u64cd\u4f5c\u6240\u9700\u7684\u5185\u5b58\u8d44\u6e90\u65f6&#xff0c;\u5c06\u51fa\u73b0 OutOfMemoryError\u3002\u8fd9\u4e9b\u5f02\u5e38\u53d1\u751f\u65f6&#xff0c;Java\u865a\u62df\u673a&#xff08;JVM&#xff09;\u4e00\u822c\u4f1a\u9009\u62e9\u7ebf\u7a0b\u7ec8\u6b62\u3002 \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&#xff0c;\u5982Java\u865a\u62df\u673a\u8fd0\u884c\u9519\u8bef&#xff08;Virtual MachineError&#xff09;\u3001\u7c7b\u5b9a\u4e49\u9519\u8bef&#xff08;NoClassDefFoundError&#xff09;\u7b49\u3002\u8fd9\u4e9b\u9519\u8bef\u662f\u4e0d\u53ef\u67e5\u7684&#xff0c;\u56e0\u4e3a\u5b83\u4eec\u5728\u5e94\u7528\u7a0b\u5e8f\u7684\u63a7\u5236\u548c\u5904\u7406\u80fd\u529b\u4e4b \u5916&#xff0c;\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&#xff0c;\u5373\u4f7f\u786e\u5b9e\u53d1\u751f\u4e86\u9519\u8bef&#xff0c;\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&#xff0c;\u9519\u8bef\u901a\u8fc7Error\u7684\u5b50\u7c7b\u63cf\u8ff0\u3002  \u6ce8\u610f&#xff1a;\u5f02\u5e38\u548c\u9519\u8bef\u7684\u533a\u522b&#xff1a;\u5f02\u5e38\u80fd\u88ab\u7a0b\u5e8f\u672c\u8eab\u53ef\u4ee5\u5904\u7406&#xff0c;\u9519\u8bef\u662f\u65e0\u6cd5\u5904\u7406\u7684\u3002<\/p>\n<h2>3. try-catch \u5904\u7406\u5f02\u5e38<\/h2>\n<p><span class=\"token keyword\">try<\/span><span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/\u53ef\u80fd\u51fa\u73b0\u5f02\u5e38\u7684\u4ee3\u7801<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token keyword\">catch<\/span><span class=\"token punctuation\">(<\/span>\u5f02\u5e38\u7c7b\u578b e<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/\u5904\u7406\u5f02\u5e38<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u4f8b\u5982&#xff1a;<\/p>\n<p><span class=\"token keyword\">public<\/span> <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Test<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">public<\/span> <span class=\"token keyword\">static<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">String<\/span><span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> args<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">int<\/span> a <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">10<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">int<\/span> b <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">try<\/span><span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span>a <span class=\"token operator\">\/<\/span> b<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u51fa\u73b0\u5f02\u5e38\u540e\u9762\u7684\u4ee3\u7801&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><span class=\"token keyword\">catch<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">ArithmeticException<\/span> e<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u9664\u6570\u4e0d\u80fd\u4e3a0&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u6267\u884c\u540e\u7eed\u4ee3\u7801&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260801072444-6a6d9f3c50af1.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" width=\"500\" height=\"340\" \/><\/p>\n<p>\u6355\u6349\u5230\u5f02\u5e38\u540e&#xff0c;\u4ecd\u7136\u4f1a\u6267\u884c\u540e\u9762\u7684\u4ee3\u7801,\u5982\u679c\u5f02\u5e38\u53d1\u751f&#xff0c;\u53d1\u751f\u5f02\u5e38\u540e\u9762\u7684\u4ee3\u7801\u4e0d\u4f1a\u6267\u884c&#xff0c;\u4f1a\u76f4\u63a5\u88ab\u8df3\u8fc7\u5230catch<\/p>\n<p><span class=\"token keyword\">public<\/span> <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Test<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">public<\/span> <span class=\"token keyword\">static<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">String<\/span><span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> args<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">int<\/span> a <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">10<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">int<\/span> b <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">try<\/span><span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span>a <span class=\"token operator\">\/<\/span> b<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><span class=\"token keyword\">catch<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">NullPointerException<\/span> e<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;NULL\u6307\u9488\u5f02\u5e38&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u6267\u884c\u540e\u7eed\u4ee3\u7801&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260801072444-6a6d9f3c7c541.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" width=\"500\" height=\"400\" \/><\/p>\n<p>\u672a catch \u5230\u7684\u5f02\u5e38&#xff0c; JVM \u4f1a\u8fdb\u884c\u5904\u7406&#xff0c;\u4e5f\u5c31\u662f\u53d1\u751f\u8fd0\u884c\u65f6\u5f02\u5e38&#xff0c;\u4f7f\u7528 catch \u8fdb\u884c\u6355\u6349\u65f6&#xff0c;\u6ce8\u610f\u5bf9\u5e94\u7684\u5f02\u5e38\u7c7b\u578b<\/p>\n<p><span class=\"token keyword\">public<\/span> <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Test<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">public<\/span> <span class=\"token keyword\">static<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">String<\/span><span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> args<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">int<\/span> a <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">10<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">int<\/span> b <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">try<\/span><span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span>a <span class=\"token operator\">\/<\/span> b<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u51fa\u73b0\u5f02\u5e38\u540e\u9762\u7684\u4ee3\u7801&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><span class=\"token keyword\">catch<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">ArithmeticException<\/span> e<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<\/span><br \/>\n            e<span class=\"token punctuation\">.<\/span><span class=\"token function\">printStackTrace<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u9664\u6570\u4e0d\u80fd\u4e3a0&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u6267\u884c\u540e\u7eed\u4ee3\u7801&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260801072444-6a6d9f3ca9bd3.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" width=\"500\" height=\"400\" \/><\/p>\n<p>\u4f7f\u7528 printStackTrace() \u5feb\u901f\u5b9a\u4f4d\u51fa\u73b0\u5f02\u5e38\u7684\u4f4d\u7f6e&#xff0c;\u4f46\u662f\u4e0d\u5f71\u54cd\u7684\u540e\u7eed\u4ee3\u7801\u7684\u6267\u884c<\/p>\n<p><span class=\"token keyword\">public<\/span> <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Test<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">public<\/span> <span class=\"token keyword\">static<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">String<\/span><span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> args<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">try<\/span><span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token keyword\">int<\/span><span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> arr <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token keyword\">int<\/span><span class=\"token punctuation\">[<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span>arr<span class=\"token punctuation\">[<\/span><span class=\"token number\">3<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><span class=\"token keyword\">catch<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">ArrayIndexOutOfBoundsException<\/span> e<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><span class=\"token keyword\">catch<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token class-name\">NullPointerException<\/span> e<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u6570\u7ec4\u7a7a\u6307\u9488\u5f02\u5e38&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u6267\u884c\u540e\u7eed\u4ee3\u7801&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260801072444-6a6d9f3cd9d2a.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" width=\"500\" height=\"400\" \/><\/p>\n<p>catch \u53ef\u80fd\u51fa\u73b0\u7684\u591a\u4e2a\u5f02\u5e38\u4e5f\u662f\u53ef\u4ee5\u7684,\u4f46\u662f\u4e0d\u4f1a\u540c\u65f6\u51fa\u73b02\u4e2a\u4ee52\u4e2a\u4ee5\u4e0a\u7684\u5f02\u5e38&#xff0c;\u56e0\u4e3a\u68c0\u6d4b\u5230\u5f02\u5e38\u5c31\u4f1a\u8df3\u8fc7\u5269\u4f59\u7684\u4ee3\u7801<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u76ee\u5f55 \u6587\u7ae0\u6458\u89811. \u4ec0\u4e48\u662f\u5f02\u5e382. \u5f02\u5e38&#xff08;Exception&#xff09;3. \u9519\u8bef&#xff08;Error&#xff09;3. try-catch \u5904\u7406\u5f02\u5e38 \u6587\u7ae0\u6458\u8981 \u672c\u6587\u4ecb\u7ecd\u4e86Java\u4e2d\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\u3002 \u4e3b\u8981\u5185\u5bb9\u5305\u62ec&#xff1a; 1.\u5f02\u5e38\u7684\u6982\u5ff5&#xff0c;\u5373\u5728\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u7684\u4e0d\u6b63\u5e38\u60c5\u51b5&#xff0c;\u5982\u7b97\u672f\u5f02\u5e38\u3001\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38\u7b49\u5e38\u89c1\u7c7b\u578b&#xff1b; 2.\u5f02\u5e38\u5206\u7c7b&#xff0c;\u5206\u4e3a\u7f16\u8bd1\u65f6\u5f02\u5e38&#xff08;\u5982IOException&amp;#xff<\/p>\n","protected":false},"author":2,"featured_media":88664,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[99],"topic":[],"class_list":["post-88670","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java \u5f02\u5e38\u2460 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/88670.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java \u5f02\u5e38\u2460 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u76ee\u5f55 \u6587\u7ae0\u6458\u89811. \u4ec0\u4e48\u662f\u5f02\u5e382. \u5f02\u5e38&#xff08;Exception&#xff09;3. \u9519\u8bef&#xff08;Error&#xff09;3. try-catch \u5904\u7406\u5f02\u5e38 \u6587\u7ae0\u6458\u8981 \u672c\u6587\u4ecb\u7ecd\u4e86Java\u4e2d\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\u3002 \u4e3b\u8981\u5185\u5bb9\u5305\u62ec&#xff1a; 1.\u5f02\u5e38\u7684\u6982\u5ff5&#xff0c;\u5373\u5728\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u7684\u4e0d\u6b63\u5e38\u60c5\u51b5&#xff0c;\u5982\u7b97\u672f\u5f02\u5e38\u3001\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38\u7b49\u5e38\u89c1\u7c7b\u578b&#xff1b; 2.\u5f02\u5e38\u5206\u7c7b&#xff0c;\u5206\u4e3a\u7f16\u8bd1\u65f6\u5f02\u5e38&#xff08;\u5982IOException&amp;#xff\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/88670.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-08-01T07:24:46+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260801072444-6a6d9f3c02bbe.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/88670.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/88670.html\",\"name\":\"Java \u5f02\u5e38\u2460 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-08-01T07:24:46+00:00\",\"dateModified\":\"2026-08-01T07:24:46+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/88670.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/88670.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/88670.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java \u5f02\u5e38\u2460\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java \u5f02\u5e38\u2460 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/88670.html","og_locale":"zh_CN","og_type":"article","og_title":"Java \u5f02\u5e38\u2460 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u76ee\u5f55 \u6587\u7ae0\u6458\u89811. \u4ec0\u4e48\u662f\u5f02\u5e382. \u5f02\u5e38&#xff08;Exception&#xff09;3. \u9519\u8bef&#xff08;Error&#xff09;3. try-catch \u5904\u7406\u5f02\u5e38 \u6587\u7ae0\u6458\u8981 \u672c\u6587\u4ecb\u7ecd\u4e86Java\u4e2d\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\u3002 \u4e3b\u8981\u5185\u5bb9\u5305\u62ec&#xff1a; 1.\u5f02\u5e38\u7684\u6982\u5ff5&#xff0c;\u5373\u5728\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u7684\u4e0d\u6b63\u5e38\u60c5\u51b5&#xff0c;\u5982\u7b97\u672f\u5f02\u5e38\u3001\u6570\u7ec4\u8d8a\u754c\u5f02\u5e38\u7b49\u5e38\u89c1\u7c7b\u578b&#xff1b; 2.\u5f02\u5e38\u5206\u7c7b&#xff0c;\u5206\u4e3a\u7f16\u8bd1\u65f6\u5f02\u5e38&#xff08;\u5982IOException&amp;#xff","og_url":"https:\/\/www.wsisp.com\/helps\/88670.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-08-01T07:24:46+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260801072444-6a6d9f3c02bbe.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/88670.html","url":"https:\/\/www.wsisp.com\/helps\/88670.html","name":"Java \u5f02\u5e38\u2460 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-08-01T07:24:46+00:00","dateModified":"2026-08-01T07:24:46+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/88670.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/88670.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/88670.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Java \u5f02\u5e38\u2460"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/88670","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=88670"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/88670\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/88664"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=88670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=88670"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=88670"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=88670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}