{"id":208391,"date":"2025-09-02T10:11:28","date_gmt":"2025-09-02T02:11:28","guid":{"rendered":"https:\/\/www.server.hk\/blog\/?p=208391"},"modified":"2025-09-02T10:11:28","modified_gmt":"2025-09-02T02:11:28","slug":"%e6%b7%b1%e5%85%a5%e6%8e%8c%e6%8f%a1-java-%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e4%bb%a5%e5%88%9b%e5%bb%ba%e5%ba%94%e7%94%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208391\/","title":{"rendered":"\u6df1\u5165\u638c\u63e1 Java \u5f02\u5e38\u5904\u7406\u4ee5\u521b\u5efa\u5e94\u7528"},"content":{"rendered":"<h2 dir=\"auto\">Java \u5f02\u5e38\u7b80\u4ecb<\/h2>\n<p dir=\"auto\">\u5f02\u5e38\u5904\u7406\u662f Java \u7f16\u7a0b\u4e2d\u5065\u58ee\u6027\u7684\u57fa\u77f3\uff0c\u4f7f\u5f00\u53d1\u8005\u80fd\u591f\u6709\u6548\u7ba1\u7406\u9519\u8bef\u548c\u610f\u5916\u60c5\u51b5\u3002\u5728 Java \u4e2d\uff0c\u5f02\u5e38\u662f\u8868\u793a\u7a0b\u5e8f\u6267\u884c\u671f\u95f4\u9519\u8bef\u6216\u610f\u5916\u4e8b\u4ef6\u7684\u5bf9\u50cf\u3002\u672c\u6587\u4e3a\u6280\u672f\u53d7\u4f17\u63d0\u4f9b\u4e86\u4e00\u4efd\u5168\u9762\u7684\u6307\u5357\uff0c\u6db5\u76d6 Java \u5f02\u5e38\u4f53\u7cfb\u7ed3\u6784\u3001\u5904\u7406\u673a\u5236\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u65e8\u5728\u5e2e\u52a9\u5f00\u53d1\u8005\u6784\u5efa\u53ef\u9760\u7684\u5e94\u7528\u3002<\/p>\n<h2 dir=\"auto\">Java \u5f02\u5e38\u4f53\u7cfb\u7ed3\u6784<\/h2>\n<p dir=\"auto\">Java \u7684\u5f02\u5e38\u7cfb\u7edf\u4ee5 Throwable \u7c7b\u4e3a\u6838\u5fc3\uff0c\u5b83\u662f\u6240\u6709\u9519\u8bef\u548c\u5f02\u5e38\u7684\u6839\u7c7b\u3002\u7406\u89e3\u8fd9\u4e00\u4f53\u7cfb\u7ed3\u6784\u5bf9\u4e8e\u6709\u6548\u5904\u7406\u9519\u8bef\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<h3 dir=\"auto\">Throwable \u7c7b<\/h3>\n<p dir=\"auto\">Throwable \u662f Java \u4e2d\u6240\u6709\u9519\u8bef\u548c\u5f02\u5e38\u7684\u8d85\u7c7b\u3002\u53ea\u6709 Throwable \u6216\u5176\u5b50\u7c7b\u7684\u5b9e\u4f8b\u53ef\u4ee5\u88ab\u629b\u51fa\u6216\u6355\u83b7\u3002\u5b83\u63d0\u4f9b\u4e86\u7528\u4e8e\u8c03\u8bd5\u548c\u9519\u8bef\u5206\u6790\u7684\u5173\u952e\u65b9\u6cd5\uff1a<\/p>\n<ul dir=\"auto\">\n<li><strong>fillInStackTrace()<\/strong>\uff1a\u6355\u83b7\u5f53\u524d\u8c03\u7528\u6808\uff0c\u589e\u5f3a\u5806\u6808\u8ddf\u8e2a\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002<\/li>\n<li><strong>getMessage()<\/strong>\uff1a\u8fd4\u56de\u6709\u5173\u5f02\u5e38\u7684\u8be6\u7ec6\u6d88\u606f\u3002<\/li>\n<li><strong>getCause()<\/strong>\uff1a\u83b7\u53d6\u5f02\u5e38\u7684\u6839\u672c\u539f\u56e0\uff08\u5982\u679c\u5b58\u5728\uff09\u3002<\/li>\n<li><strong>getStackTrace()<\/strong>\uff1a\u8fd4\u56de\u5806\u6808\u8ddf\u8e2a\u5143\u7d20\u7684\u6570\u7ec4\uff0c\u7d22\u5f15 0 \u8868\u793a\u6808\u9876\u3002<\/li>\n<li><strong>printStackTrace()<\/strong>\uff1a\u5c06\u5806\u6808\u8ddf\u8e2a\u8f93\u51fa\u5230\u6807\u51c6\u9519\u8bef\u6d41\u3002<\/li>\n<li><strong>toString()<\/strong>\uff1a\u63d0\u4f9b\u5f02\u5e38\u7684\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u5305\u62ec\u5176\u6d88\u606f\u3002<\/li>\n<\/ul>\n<h3 dir=\"auto\">Error \u4e0e Exception<\/h3>\n<p dir=\"auto\">Throwable \u6709\u4e24\u4e2a\u4e3b\u8981\u5b50\u7c7b\uff1aError \u548c Exception\u3002<\/p>\n<ul dir=\"auto\">\n<li><strong>Error<\/strong>\uff1a\u8868\u793a\u5e94\u7528\u7a0b\u5e8f\u901a\u5e38\u65e0\u6cd5\u6062\u590d\u7684\u4e25\u91cd\u95ee\u9898\uff0c\u4f8b\u5982 OutOfMemoryError \u6216 StackOverflowError\u3002\u8fd9\u4e9b\u5f02\u5e38\u4e0d\u53d7\u7f16\u8bd1\u5668\u68c0\u67e5\uff0c\u901a\u5e38\u4e0d\u9700\u8981\u6355\u83b7\uff0c\u56e0\u4e3a\u5b83\u4eec\u8868\u793a\u7cfb\u7edf\u7ea7\u6545\u969c\u3002<\/li>\n<li><strong>Exception<\/strong>\uff1a\u8868\u793a\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u5904\u7406\u7684\u6761\u4ef6\u3002\u5f02\u5e38\u5206\u4e3a\uff1a\n<ul dir=\"auto\">\n<li><strong>\u53d7\u68c0\u5f02\u5e38\uff08Checked Exceptions\uff09<\/strong>\uff1aException \u7684\u5b50\u7c7b\uff08\u4e0d\u5305\u62ec RuntimeException\uff09\uff0c\u5fc5\u987b\u901a\u8fc7 throws \u58f0\u660e\u6216\u7528 try-catch \u5757\u5904\u7406\u3002\u4f8b\u5982 IOException \u548c SQLException\u3002<\/li>\n<li><strong>\u975e\u53d7\u68c0\u5f02\u5e38\uff08Unchecked Exceptions\uff09<\/strong>\uff1aRuntimeException \u7684\u5b50\u7c7b\uff0c\u65e0\u9700\u663e\u5f0f\u5904\u7406\u3002\u4f8b\u5982 NullPointerException \u548c ArithmeticException\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4 dir=\"auto\">\u5e38\u89c1\u9519\u8bef<\/h4>\n<div dir=\"auto\">\n<div>\n<div><\/div>\n<\/div>\n<table dir=\"auto\" data-wide=\"false\">\n<thead>\n<tr>\n<th data-col-size=\"lg\">\u9519\u8bef\u7c7b\u578b<\/th>\n<th data-col-size=\"md\">\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"lg\">AssertionError<\/td>\n<td data-col-size=\"md\">\u65ad\u8a00\u5931\u8d25\u65f6\u629b\u51fa\u3002<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">OutOfMemoryError<\/td>\n<td data-col-size=\"md\">\u5185\u5b58\u4e0d\u8db3\u65f6\u629b\u51fa\u3002<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">StackOverflowError<\/td>\n<td data-col-size=\"md\">\u9012\u5f52\u8fc7\u6df1\u5bfc\u81f4\u6808\u6ea2\u51fa\u65f6\u629b\u51fa\u3002<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">VirtualMachineError<\/td>\n<td data-col-size=\"md\">\u8868\u793a JVM \u7ea7\u522b\u7684\u6545\u969c\u3002<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">UnsupportedClassVersionError<\/td>\n<td data-col-size=\"md\">\u8fd0\u884c\u4e0e\u4e0d\u517c\u5bb9 JVM \u7248\u672c\u7f16\u8bd1\u7684\u7c7b\u65f6\u629b\u51fa\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h4 dir=\"auto\">\u5e38\u89c1\u53d7\u68c0\u5f02\u5e38<\/h4>\n<div dir=\"auto\">\n<div>\n<div><\/div>\n<\/div>\n<table dir=\"auto\" data-wide=\"false\">\n<thead>\n<tr>\n<th data-col-size=\"lg\">\u5f02\u5e38\u7c7b\u578b<\/th>\n<th data-col-size=\"md\">\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"lg\">ClassNotFoundException<\/td>\n<td data-col-size=\"md\">\u627e\u4e0d\u5230\u7c7b\u65f6\u629b\u51fa\u3002<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">IOException<\/td>\n<td data-col-size=\"md\">I\/O \u64cd\u4f5c\u5931\u8d25\u65f6\u629b\u51fa\u3002<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">InterruptedException<\/td>\n<td data-col-size=\"md\">\u7ebf\u7a0b\u88ab\u4e2d\u65ad\u65f6\u629b\u51fa\u3002<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">NoSuchMethodException<\/td>\n<td data-col-size=\"md\">\u627e\u4e0d\u5230\u65b9\u6cd5\u65f6\u629b\u51fa\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h4 dir=\"auto\">\u5e38\u89c1\u975e\u53d7\u68c0\u5f02\u5e38<\/h4>\n<div dir=\"auto\">\n<div>\n<div><\/div>\n<\/div>\n<table dir=\"auto\" data-wide=\"false\">\n<thead>\n<tr>\n<th data-col-size=\"lg\">\u5f02\u5e38\u7c7b\u578b<\/th>\n<th data-col-size=\"md\">\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"lg\">NullPointerException<\/td>\n<td data-col-size=\"md\">\u8bbf\u95ee\u7a7a\u5bf9\u8c61\u5f15\u7528\u65f6\u629b\u51fa\u3002<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">ArithmeticException<\/td>\n<td data-col-size=\"md\">\u6570\u5b66\u9519\u8bef\u65f6\u629b\u51fa\uff0c\u4f8b\u5982\u9664\u4ee5\u96f6\u3002<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">ArrayIndexOutOfBoundsException<\/td>\n<td data-col-size=\"md\">\u8bbf\u95ee\u65e0\u6548\u6570\u7ec4\u7d22\u5f15\u65f6\u629b\u51fa\u3002<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">ClassCastException<\/td>\n<td data-col-size=\"md\">\u7c7b\u578b\u8f6c\u6362\u65e0\u6548\u65f6\u629b\u51fa\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2 dir=\"auto\">Java \u4e2d\u7684\u5f02\u5e38\u5904\u7406<\/h2>\n<p dir=\"auto\">Java \u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u673a\u5236\u6765\u629b\u51fa\u548c\u6355\u83b7\u5f02\u5e38\uff0c\u786e\u4fdd\u7a0b\u5e8f\u80fd\u591f\u4f18\u96c5\u5730\u4ece\u9519\u8bef\u4e2d\u6062\u590d\u3002<\/p>\n<h3 dir=\"auto\">\u4f7f\u7528 throw \u548c throws \u629b\u51fa\u5f02\u5e38<\/h3>\n<ul dir=\"auto\">\n<li><strong>throw<\/strong>\uff1a\u5728\u65b9\u6cd5\u5185\u663e\u5f0f\u629b\u51fa\u5f02\u5e38\u5bf9\u8c61\u3002<\/li>\n<li><strong>throws<\/strong>\uff1a\u5728\u65b9\u6cd5\u7b7e\u540d\u4e2d\u58f0\u660e\u65b9\u6cd5\u53ef\u80fd\u629b\u51fa\u7684\u5f02\u5e38\u3002<\/li>\n<\/ul>\n<p dir=\"auto\"><strong>\u793a\u4f8b\uff1a\u629b\u51fa\u5f02\u5e38<\/strong><\/p>\n<div dir=\"auto\">\n<div>\n<div>java<\/div>\n<div>\n<div>\n<div><\/div>\n<\/div>\n<\/div>\n<div><code><\/code><\/p>\n<div>\n<div>\n<div>public class ExceptionExample {<\/div>\n<div>public static void validateInput(int value) throws IllegalArgumentException {<\/div>\n<div>if (value &lt; 0) {<\/div>\n<div>throw new IllegalArgumentException(&#8220;\u8f93\u5165\u5fc5\u987b\u4e3a\u975e\u8d1f\u6570&#8221;);<\/div>\n<div>}<\/div>\n<div>}<\/div>\n<div><\/div>\n<div>public static void main(String[] args) {<\/div>\n<div>try {<\/div>\n<div>validateInput(-5);<\/div>\n<div>} catch (IllegalArgumentException e) {<\/div>\n<div>System.out.println(&#8220;\u9519\u8bef\uff1a&#8221; + e.getMessage());<\/div>\n<div>}<\/div>\n<div>}<\/div>\n<div>}<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>\u8f93\u51fa<\/strong>\uff1a<\/p>\n<div dir=\"auto\">\n<div>\n<div>text<\/div>\n<div>\n<div>\n<div><\/div>\n<\/div>\n<\/div>\n<div><code><\/code><\/p>\n<div>\n<div>\n<div>\u9519\u8bef\uff1a\u8f93\u5165\u5fc5\u987b\u4e3a\u975e\u8d1f\u6570<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">\u4f7f\u7528 try-catch-finally \u6355\u83b7\u5f02\u5e38<\/h3>\n<p dir=\"auto\">try-catch-finally \u7ed3\u6784\u7528\u4e8e\u5904\u7406\u5f02\u5e38\uff1a<\/p>\n<ul dir=\"auto\">\n<li><strong>try<\/strong>\uff1a\u5305\u542b\u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801\u3002<\/li>\n<li><strong>catch<\/strong>\uff1a\u5904\u7406 try \u5757\u4e2d\u629b\u51fa\u7684\u7279\u5b9a\u5f02\u5e38\u7c7b\u578b\u3002<\/li>\n<li><strong>finally<\/strong>\uff1a\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\u90fd\u4f1a\u6267\u884c\uff0c\u9002\u5408\u7528\u4e8e\u8d44\u6e90\u6e05\u7406\u3002<\/li>\n<\/ul>\n<p dir=\"auto\"><strong>\u793a\u4f8b\uff1a\u4f7f\u7528 try-catch-finally<\/strong><\/p>\n<div dir=\"auto\">\n<div>\n<div>java<\/div>\n<div>\n<div>\n<div><\/div>\n<\/div>\n<\/div>\n<div><code><\/code><\/p>\n<div>\n<div>\n<div>public class TryCatchExample {<\/div>\n<div>public static void main(String[] args) {<\/div>\n<div>try {<\/div>\n<div>int result = 10 \/ 0; \/\/ \u89e6\u53d1 ArithmeticException<\/div>\n<div>System.out.println(&#8220;\u7ed3\u679c\uff1a&#8221; + result);<\/div>\n<div>} catch (ArithmeticException e) {<\/div>\n<div>System.out.println(&#8220;\u9519\u8bef\uff1a\u9664\u4ee5\u96f6&#8221;);<\/div>\n<div>} finally {<\/div>\n<div>System.out.println(&#8220;\u6e05\u7406\u64cd\u4f5c\u5df2\u6267\u884c&#8221;);<\/div>\n<div>}<\/div>\n<div>}<\/div>\n<div>}<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>\u8f93\u51fa<\/strong>\uff1a<\/p>\n<div dir=\"auto\">\n<div>\n<div>text<\/div>\n<div>\n<div>\n<div><\/div>\n<\/div>\n<\/div>\n<div><code><\/code><\/p>\n<div>\n<div>\n<div>\u9519\u8bef\uff1a\u9664\u4ee5\u96f6<\/div>\n<div>\u6e05\u7406\u64cd\u4f5c\u5df2\u6267\u884c<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">\u591a\u5f02\u5e38\u6355\u83b7\uff08JDK 7+\uff09<\/h4>\n<p dir=\"auto\">\u4ece JDK 7 \u5f00\u59cb\uff0c\u5355\u4e2a catch \u5757\u53ef\u4ee5\u6355\u83b7\u591a\u79cd\u5f02\u5e38\uff1a<\/p>\n<div dir=\"auto\">\n<div>\n<div>java<\/div>\n<div>\n<div>\n<div><\/div>\n<\/div>\n<\/div>\n<div><code><\/code><\/p>\n<div>\n<div>\n<div>try {<\/div>\n<div>\/\/ \u53ef\u80fd\u629b\u51fa\u591a\u79cd\u5f02\u5e38\u7684\u4ee3\u7801<\/div>\n<div>} catch (IOException | SQLException e) {<\/div>\n<div>e.printStackTrace();<\/div>\n<div>}<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">try-catch-finally \u7684\u5173\u952e\u6ce8\u610f\u4e8b\u9879<\/h3>\n<ul dir=\"auto\">\n<li><strong>catch \u5757\u7684\u987a\u5e8f<\/strong>\uff1a\u5c06\u5177\u4f53\u5f02\u5e38\u653e\u5728\u901a\u7528\u5f02\u5e38\u4e4b\u524d\uff08\u4f8b\u5982\uff0c\u5148\u6355\u83b7 ArithmeticException\uff0c\u518d\u6355\u83b7 RuntimeException\uff09\uff0c\u4ee5\u907f\u514d\u4e0d\u53ef\u8fbe\u4ee3\u7801\u3002<\/li>\n<li><strong>\u907f\u514d\u5728 finally \u4e2d\u8fd4\u56de<\/strong>\uff1afinally \u5757\u4e2d\u7684 return \u6216 throw \u53ef\u80fd\u8986\u76d6 catch \u5757\u7684\u884c\u4e3a\uff0c\u5bfc\u81f4\u9519\u8bef\u88ab\u63a9\u76d6\u3002<\/li>\n<li><strong>\u8d44\u6e90\u7ba1\u7406<\/strong>\uff1a\u5728 finally \u5757\u4e2d\u5173\u95ed\u8d44\u6e90\uff0c\u5982\u6570\u636e\u5e93\u8fde\u63a5\u6216\u6587\u4ef6\u6d41\u3002<\/li>\n<\/ul>\n<h2 dir=\"auto\">\u81ea\u5b9a\u4e49\u5f02\u5e38<\/h2>\n<p dir=\"auto\">\u81ea\u5b9a\u4e49\u5f02\u5e38\u901a\u8fc7\u5b9a\u4e49\u7279\u5b9a\u9886\u57df\u7684\u9519\u8bef\u6761\u4ef6\u6765\u63d0\u9ad8\u4ee3\u7801\u6e05\u6670\u5ea6\u3002\u901a\u5e38\u7ee7\u627f Exception\uff08\u53d7\u68c0\u5f02\u5e38\uff09\u6216 RuntimeException\uff08\u975e\u53d7\u68c0\u5f02\u5e38\uff09\u3002<\/p>\n<p dir=\"auto\"><strong>\u793a\u4f8b\uff1a\u81ea\u5b9a\u4e49\u5f02\u5e38<\/strong><\/p>\n<div dir=\"auto\">\n<div>\n<div>java<\/div>\n<div>\n<div>\n<div><\/div>\n<\/div>\n<\/div>\n<div><code><\/code><\/p>\n<div>\n<div>\n<div>public class CustomExceptionExample {<\/div>\n<div>static class InsufficientBalanceException extends Exception {<\/div>\n<div>public InsufficientBalanceException(String message) {<\/div>\n<div>super(message);<\/div>\n<div>}<\/div>\n<div>}<\/div>\n<div><\/div>\n<div>public static void withdraw(double balance, double amount) throws InsufficientBalanceException {<\/div>\n<div>if (amount &gt; balance) {<\/div>\n<div>throw new InsufficientBalanceException(&#8220;\u4f59\u989d\u4e0d\u8db3\uff0c\u65e0\u6cd5\u63d0\u73b0&#8221;);<\/div>\n<div>}<\/div>\n<div>System.out.println(&#8220;\u63d0\u73b0\u6210\u529f&#8221;);<\/div>\n<div>}<\/div>\n<div><\/div>\n<div>public static void main(String[] args) {<\/div>\n<div>try {<\/div>\n<div>withdraw(100.0, 150.0);<\/div>\n<div>} catch (InsufficientBalanceException e) {<\/div>\n<div>System.out.println(&#8220;\u9519\u8bef\uff1a&#8221; + e.getMessage());<\/div>\n<div>}<\/div>\n<div>}<\/div>\n<div>}<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>\u8f93\u51fa<\/strong>\uff1a<\/p>\n<div dir=\"auto\">\n<div>\n<div>text<\/div>\n<div>\n<div>\n<div><\/div>\n<\/div>\n<\/div>\n<div><code><\/code><\/p>\n<div>\n<div>\n<div>\u9519\u8bef\uff1a\u4f59\u989d\u4e0d\u8db3\uff0c\u65e0\u6cd5\u63d0\u73b0<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<h2 dir=\"auto\">\u5f02\u5e38\u94fe<\/h2>\n<p dir=\"auto\">\u5f02\u5e38\u94fe\u5141\u8bb8\u5c06\u4e00\u4e2a\u5f02\u5e38\u5305\u88c5\u4e3a\u53e6\u4e00\u4e2a\u5f02\u5e38\u7684\u539f\u56e0\uff0c\u4fdd\u7559\u539f\u59cb\u9519\u8bef\u4e0a\u4e0b\u6587\u3002\u8fd9\u63d0\u9ad8\u4e86\u8c03\u8bd5\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<p dir=\"auto\"><strong>\u793a\u4f8b\uff1a\u5f02\u5e38\u94fe<\/strong><\/p>\n<div dir=\"auto\">\n<div>\n<div>java<\/div>\n<div>\n<div>\n<div><\/div>\n<\/div>\n<\/div>\n<div><code><\/code><\/p>\n<div>\n<div>\n<div>public class ExceptionChainExample {<\/div>\n<div>static class DatabaseException extends Exception {<\/div>\n<div>public DatabaseException(String message, Throwable cause) {<\/div>\n<div>super(message, cause);<\/div>\n<div>}<\/div>\n<div>}<\/div>\n<div><\/div>\n<div>public static void accessDatabase() throws DatabaseException {<\/div>\n<div>try {<\/div>\n<div>Class.forName(&#8220;com.example.NonExistentDriver&#8221;); \/\/ \u6a21\u62df\u9a71\u52a8\u672a\u627e\u5230<\/div>\n<div>} catch (ClassNotFoundException e) {<\/div>\n<div>throw new DatabaseException(&#8220;\u65e0\u6cd5\u8fde\u63a5\u6570\u636e\u5e93&#8221;, e);<\/div>\n<div>}<\/div>\n<div>}<\/div>\n<div><\/div>\n<div>public static void main(String[] args) {<\/div>\n<div>try {<\/div>\n<div>accessDatabase();<\/div>\n<div>} catch (DatabaseException e) {<\/div>\n<div>e.printStackTrace();<\/div>\n<div>}<\/div>\n<div>}<\/div>\n<div>}<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>\u8f93\u51fa<\/strong>\uff1a<\/p>\n<div dir=\"auto\">\n<div>\n<div>text<\/div>\n<div>\n<div>\n<div><\/div>\n<\/div>\n<\/div>\n<div><code><\/code><\/p>\n<div>\n<div>\n<div>DatabaseException: \u65e0\u6cd5\u8fde\u63a5\u6570\u636e\u5e93<\/div>\n<div>at ExceptionChainExample.accessDatabase(ExceptionChainExample.java:10)<\/div>\n<div>&#8230;<\/div>\n<div>Caused by: java.lang.ClassNotFoundException: com.example.NonExistentDriver<\/div>\n<div>&#8230;<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<h2 dir=\"auto\">\u5f02\u5e38\u5904\u7406\u6700\u4f73\u5b9e\u8df5<\/h2>\n<p dir=\"auto\">\u4e3a\u786e\u4fdd\u4ee3\u7801\u5065\u58ee\u4e14\u6613\u4e8e\u7ef4\u62a4\uff0c\u8bf7\u9075\u5faa\u4ee5\u4e0b\u6307\u5357\uff1a<\/p>\n<ol dir=\"auto\">\n<li><strong>\u5bf9\u53ef\u6062\u590d\u60c5\u51b5\u4f7f\u7528\u53d7\u68c0\u5f02\u5e38<\/strong>\uff1a\u5c06\u53d7\u68c0\u5f02\u5e38\u7528\u4e8e\u53ef\u80fd\u6062\u590d\u7684\u573a\u666f\uff0c\u4f8b\u5982\u6587\u4ef6 I\/O \u9519\u8bef\u3002<\/li>\n<li><strong>\u5bf9\u7f16\u7a0b\u9519\u8bef\u4f7f\u7528\u975e\u53d7\u68c0\u5f02\u5e38<\/strong>\uff1a\u5bf9\u7a7a\u6307\u9488\u6216\u65e0\u6548\u53c2\u6570\u7b49\u9519\u8bef\u4f7f\u7528 RuntimeException \u5b50\u7c7b\u3002<\/li>\n<li><strong>\u4f18\u5148\u4f7f\u7528\u6807\u51c6\u5f02\u5e38<\/strong>\uff1a\u5728\u9002\u5f53\u60c5\u51b5\u4e0b\u4f7f\u7528\u5185\u7f6e\u5f02\u5e38\uff0c\u5982 IllegalArgumentException\uff0c\u800c\u975e\u81ea\u5b9a\u4e49\u5f02\u5e38\u3002<\/li>\n<li><strong>\u63d0\u4f9b\u8be6\u7ec6\u7684\u6d88\u606f<\/strong>\uff1a\u5f02\u5e38\u6d88\u606f\u5e94\u5305\u542b\u6709\u52a9\u4e8e\u8c03\u8bd5\u7684\u4fe1\u606f\u3002<\/li>\n<li><strong>\u6700\u5c0f\u5316 try \u5757\u8303\u56f4<\/strong>\uff1a\u5c06 try \u5757\u9650\u5236\u5728\u7279\u5b9a\u6613\u51fa\u9519\u7684\u4ee3\u7801\u4e0a\u3002<\/li>\n<li><strong>\u907f\u514d\u5ffd\u7565\u5f02\u5e38<\/strong>\uff1a\u59cb\u7ec8\u5904\u7406\u6216\u8bb0\u5f55\u5f02\u5e38\uff0c\u5207\u52ff\u9ed8\u9ed8\u5ffd\u7565\u3002<\/li>\n<li><strong>\u5728 finally \u4e2d\u6e05\u7406\u8d44\u6e90<\/strong>\uff1a\u786e\u4fdd\u5728 finally \u5757\u6216\u4f7f\u7528 try-with-resources \u91ca\u653e\u8d44\u6e90\u3002<\/li>\n<li><strong>\u907f\u514d\u4f7f\u7528\u5f02\u5e38\u63a7\u5236\u6d41\u7a0b<\/strong>\uff1a\u5f02\u5e38\u5f00\u9500\u5927\uff0c\u4e0d\u5e94\u66ff\u4ee3\u6761\u4ef6\u903b\u8f91\u3002<\/li>\n<li><strong>\u9002\u5f53\u8bb0\u5f55\u5f02\u5e38<\/strong>\uff1a\n<ul dir=\"auto\">\n<li><strong>\u903b\u8f91\u5f02\u5e38<\/strong>\uff1a\u63cf\u8ff0\u4e1a\u52a1\u903b\u8f91\u5931\u8d25\uff0c\u4f8b\u5982\u65e0\u6548\u7528\u6237\u8f93\u5165\u3002<\/li>\n<li><strong>\u4ee3\u7801\u9519\u8bef<\/strong>\uff1a\u6307\u4ee3\u7f16\u7a0b\u9519\u8bef\uff0c\u4f8b\u5982 NullPointerException\u3002<\/li>\n<li><strong>\u7279\u5b9a\u9886\u57df\u5f02\u5e38<\/strong>\uff1a\u4e3a\u72ec\u7279\u4e1a\u52a1\u573a\u666f\u5b9a\u4e49\u81ea\u5b9a\u4e49\u5f02\u5e38\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u591a\u7ebf\u7a0b\u4e2d\u7684\u5f02\u5e38\u5904\u7406<\/strong>\uff1a\u672a\u6355\u83b7\u7684\u5f02\u5e38\u4ec5\u7ec8\u6b62\u53d7\u5f71\u54cd\u7684\u7ebf\u7a0b\uff0c\u800c\u975e\u6574\u4e2a\u7a0b\u5e8f\u3002\u9700\u8981\u65f6\u4f7f\u7528\u7ebf\u7a0b\u7279\u5b9a\u7684\u5904\u7406\u7a0b\u5e8f\u3002<\/li>\n<\/ol>\n<h2 dir=\"auto\">\u5f02\u5e38\u5904\u7406\u4e0e\u65b9\u6cd5\u91cd\u5199<\/h2>\n<p dir=\"auto\">\u5728\u91cd\u5199\u58f0\u660e\u5f02\u5e38\u7684\u65b9\u6cd5\u65f6\uff0c\u5b50\u7c7b\u65b9\u6cd5\u5fc5\u987b\u9075\u5faa\u7236\u7c7b\u7684 throws \u5b50\u53e5\uff1a<\/p>\n<ul dir=\"auto\">\n<li>\u5b50\u7c7b\u65b9\u6cd5\u53ef\u4ee5\u629b\u51fa\u66f4\u5c11\u6216\u66f4\u5177\u4f53\u7684\u5f02\u5e38\uff0c\u4f46\u4e0d\u80fd\u629b\u51fa\u66f4\u5e7f\u7684\u5f02\u5e38\u3002<\/li>\n<li>\u8fd9\u786e\u4fdd\u4e86\u591a\u6001\u4ee3\u7801\u7684\u517c\u5bb9\u6027\u3002<\/li>\n<\/ul>\n<p dir=\"auto\"><strong>\u793a\u4f8b\uff1a\u9519\u8bef\u7684\u91cd\u5199<\/strong><\/p>\n<div dir=\"auto\">\n<div>\n<div>java<\/div>\n<div>\n<div>\n<div><\/div>\n<\/div>\n<\/div>\n<div><code><\/code><\/p>\n<div>\n<div>\n<div>class Parent {<\/div>\n<div>void process() throws IOException {<\/div>\n<div>\/\/ \u5b9e\u73b0<\/div>\n<div>}<\/div>\n<div>}<\/div>\n<div><\/div>\n<div>class Child extends Parent {<\/div>\n<div>@Override<\/div>\n<div>void process() throws SQLException { \/\/ \u7f16\u8bd1\u9519\u8bef<\/div>\n<div>\/\/ \u5b9e\u73b0<\/div>\n<div>}<\/div>\n<div>}<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>\u8bf4\u660e<\/strong>\uff1aSQLException \u4e0d\u662f IOException \u7684\u5b50\u7c7b\uff0c\u8fdd\u53cd\u4e86\u91cd\u5199\u5951\u7ea6\u3002<\/p>\n<h2 dir=\"auto\">\u7ed3\u8bba<\/h2>\n<p dir=\"auto\">\u5728 Java \u4e2d\u6709\u6548\u5904\u7406\u5f02\u5e38\u9700\u8981\u6df1\u5165\u7406\u89e3\u5f02\u5e38\u4f53\u7cfb\u7ed3\u6784\u3001\u6b63\u786e\u4f7f\u7528 throw\u3001throws \u548c try-catch-finally\uff0c\u5e76\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\u3002\u901a\u8fc7\u9002\u5f53\u4f7f\u7528\u53d7\u68c0\u548c\u975e\u53d7\u68c0\u5f02\u5e38\u3001\u521b\u5efa\u6709\u610f\u4e49\u7684\u81ea\u5b9a\u4e49\u5f02\u5e38\u4ee5\u53ca\u5229\u7528\u5f02\u5e38\u94fe\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6784\u5efa\u5065\u58ee\u3001\u53ef\u7ef4\u62a4\u7684\u5e94\u7528\u3002\u59cb\u7ec8\u4f18\u5148\u8003\u8651\u6e05\u6670\u5ea6\u3001\u6700\u5c0f\u5316\u548c\u9002\u5f53\u7684\u8d44\u6e90\u7ba1\u7406\uff0c\u4ee5\u786e\u4fdd Java \u5e94\u7528\u80fd\u591f\u4f18\u96c5\u5730\u5904\u7406\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java \u5f02\u5e38\u7b80\u4ecb \u5f02\u5e38\u5904\u7406\u662f &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-208391","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208391","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=208391"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208391\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}