{"id":5302,"date":"2021-12-21T22:28:04","date_gmt":"2021-12-21T14:28:04","guid":{"rendered":"https:\/\/www.xttblog.com\/?p=5302"},"modified":"2021-12-21T22:28:04","modified_gmt":"2021-12-21T14:28:04","slug":"java%e5%bc%82%e5%b8%b8%e9%9d%a2%e8%af%95%e9%a2%98%ef%bc%882022%e6%9c%80%e6%96%b0%e7%89%88%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=5302","title":{"rendered":"Java\u5f02\u5e38\u9762\u8bd5\u9898\uff082022\u6700\u65b0\u7248\uff09"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Java\u5f02\u5e38\u67b6\u6784\u4e0e\u5f02\u5e38\u5173\u952e\u5b57<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Java\u5f02\u5e38\u7b80\u4ecb<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Java\u5f02\u5e38\u662fJava\u63d0\u4f9b\u7684\u4e00\u79cd\u8bc6\u522b\u53ca\u54cd\u5e94\u9519\u8bef\u7684\u4e00\u81f4\u6027\u673a\u5236\u3002<br>\nJava\u5f02\u5e38\u673a\u5236\u53ef\u4ee5\u4f7f\u7a0b\u5e8f\u4e2d\u5f02\u5e38\u5904\u7406\u4ee3\u7801\u548c\u6b63\u5e38\u4e1a\u52a1\u4ee3\u7801\u5206\u79bb\uff0c\u4fdd\u8bc1\u7a0b\u5e8f\u4ee3\u7801\u66f4\u52a0\u4f18\u96c5\uff0c\u5e76\u63d0\u9ad8\u7a0b\u5e8f\u5065\u58ee\u6027\u3002\u5728\u6709\u6548\u4f7f\u7528\u5f02\u5e38\u7684\u60c5\u51b5\u4e0b\uff0c\u5f02\u5e38\u80fd\u6e05\u6670\u7684\u56de\u7b54what, where, why\u8fd93\u4e2a\u95ee\u9898\uff1a\u5f02\u5e38\u7c7b\u578b\u56de\u7b54\u4e86\u201c\u4ec0\u4e48\u201d\u88ab\u629b\u51fa\uff0c\u5f02\u5e38\u5806\u6808\u8ddf\u8e2a\u56de\u7b54\u4e86\u201c\u5728\u54ea\u201d\u629b\u51fa\uff0c\u5f02\u5e38\u4fe1\u606f\u56de\u7b54\u4e86\u201c\u4e3a\u4ec0\u4e48\u201d\u4f1a\u629b\u51fa\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Java\u5f02\u5e38\u67b6\u6784<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2021\/12\/err0.png\" alt=\"\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">1. Throwable<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Throwable \u662f Java \u8bed\u8a00\u4e2d\u6240\u6709\u9519\u8bef\u4e0e\u5f02\u5e38\u7684\u8d85\u7c7b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Throwable \u5305\u542b\u4e24\u4e2a\u5b50\u7c7b\uff1aError\uff08\u9519\u8bef\uff09\u548c Exception\uff08\u5f02\u5e38\uff09\uff0c\u5b83\u4eec\u901a\u5e38\u7528\u4e8e\u6307\u793a\u53d1\u751f\u4e86\u5f02\u5e38\u60c5\u51b5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Throwable \u5305\u542b\u4e86\u5176\u7ebf\u7a0b\u521b\u5efa\u65f6\u7ebf\u7a0b\u6267\u884c\u5806\u6808\u7684\u5feb\u7167\uff0c\u5b83\u63d0\u4f9b\u4e86 printStackTrace() \u7b49\u63a5\u53e3\u7528\u4e8e\u83b7\u53d6\u5806\u6808\u8ddf\u8e2a\u6570\u636e\u7b49\u4fe1\u606f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Error\uff08\u9519\u8bef\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u5b9a\u4e49\u300d<\/strong>\uff1aError \u7c7b\u53ca\u5176\u5b50\u7c7b\u3002\u7a0b\u5e8f\u4e2d\u65e0\u6cd5\u5904\u7406\u7684\u9519\u8bef\uff0c\u8868\u793a\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u4e2d\u51fa\u73b0\u4e86\u4e25\u91cd\u7684\u9519\u8bef\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u7279\u70b9\u300d<\/strong>\uff1a\u6b64\u7c7b\u9519\u8bef\u4e00\u822c\u8868\u793a\u4ee3\u7801\u8fd0\u884c\u65f6 JVM \u51fa\u73b0\u95ee\u9898\u3002\u901a\u5e38\u6709 Virtual MachineError\uff08\u865a\u62df\u673a\u8fd0\u884c\u9519\u8bef\uff09\u3001NoClassDefFoundError\uff08\u7c7b\u5b9a\u4e49\u9519\u8bef\uff09\u7b49\u3002\u6bd4\u5982 OutOfMemoryError\uff1a\u5185\u5b58\u4e0d\u8db3\u9519\u8bef\uff1bStackOverflowError\uff1a\u6808\u6ea2\u51fa\u9519\u8bef\u3002\u6b64\u7c7b\u9519\u8bef\u53d1\u751f\u65f6\uff0cJVM \u5c06\u7ec8\u6b62\u7ebf\u7a0b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e9b\u9519\u8bef\u662f\u4e0d\u53d7\u68c0\u5f02\u5e38\uff0c\u975e\u4ee3\u7801\u6027\u9519\u8bef\u3002\u56e0\u6b64\uff0c\u5f53\u6b64\u7c7b\u9519\u8bef\u53d1\u751f\u65f6\uff0c\u5e94\u7528\u7a0b\u5e8f\u4e0d\u5e94\u8be5\u53bb\u5904\u7406\u6b64\u7c7b\u9519\u8bef\u3002\u6309\u7167Java\u60ef\u4f8b\uff0c\u6211\u4eec\u662f\u4e0d\u5e94\u8be5\u5b9e\u73b0\u4efb\u4f55\u65b0\u7684Error\u5b50\u7c7b\u7684\uff01<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Exception\uff08\u5f02\u5e38\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u7a0b\u5e8f\u672c\u8eab\u53ef\u4ee5\u6355\u83b7\u5e76\u4e14\u53ef\u4ee5\u5904\u7406\u7684\u5f02\u5e38\u3002Exception \u8fd9\u79cd\u5f02\u5e38\u53c8\u5206\u4e3a\u4e24\u7c7b\uff1a\u8fd0\u884c\u65f6\u5f02\u5e38\u548c\u7f16\u8bd1\u65f6\u5f02\u5e38\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u8fd0\u884c\u65f6\u5f02\u5e38<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u5b9a\u4e49\u300d<\/strong>\uff1aRuntimeException \u7c7b\u53ca\u5176\u5b50\u7c7b\uff0c\u8868\u793a JVM \u5728\u8fd0\u884c\u671f\u95f4\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u7279\u70b9\u300d<\/strong>\uff1aJava \u7f16\u8bd1\u5668\u4e0d\u4f1a\u68c0\u67e5\u5b83\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f53\u7a0b\u5e8f\u4e2d\u53ef\u80fd\u51fa\u73b0\u8fd9\u7c7b\u5f02\u5e38\u65f6\uff0c\u5018\u82e5\u65e2&#8221;\u6ca1\u6709\u901a\u8fc7throws\u58f0\u660e\u629b\u51fa\u5b83&#8221;\uff0c\u4e5f&#8221;\u6ca1\u6709\u7528try-catch\u8bed\u53e5\u6355\u83b7\u5b83&#8221;\uff0c\u8fd8\u662f\u4f1a\u7f16\u8bd1\u901a\u8fc7\u3002\u6bd4\u5982NullPointerException\u7a7a\u6307\u9488\u5f02\u5e38\u3001ArrayIndexOutBoundException\u6570\u7ec4\u4e0b\u6807\u8d8a\u754c\u5f02\u5e38\u3001ClassCastException\u7c7b\u578b\u8f6c\u6362\u5f02\u5e38\u3001ArithmeticExecption\u7b97\u672f\u5f02\u5e38\u3002\u6b64\u7c7b\u5f02\u5e38\u5c5e\u4e8e\u4e0d\u53d7\u68c0\u5f02\u5e38\uff0c\u4e00\u822c\u662f\u7531\u7a0b\u5e8f\u903b\u8f91\u9519\u8bef\u5f15\u8d77\u7684\uff0c\u5728\u7a0b\u5e8f\u4e2d\u53ef\u4ee5\u9009\u62e9\u6355\u83b7\u5904\u7406\uff0c\u4e5f\u53ef\u4ee5\u4e0d\u5904\u7406\u3002\u867d\u7136 Java \u7f16\u8bd1\u5668\u4e0d\u4f1a\u68c0\u67e5\u8fd0\u884c\u65f6\u5f02\u5e38\uff0c\u4f46\u662f\u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7 throws \u8fdb\u884c\u58f0\u660e\u629b\u51fa\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7 try-catch \u5bf9\u5b83\u8fdb\u884c\u6355\u83b7\u5904\u7406\u3002\u5982\u679c\u4ea7\u751f\u8fd0\u884c\u65f6\u5f02\u5e38\uff0c\u5219\u9700\u8981\u901a\u8fc7\u4fee\u6539\u4ee3\u7801\u6765\u8fdb\u884c\u907f\u514d\u3002\u4f8b\u5982\uff0c\u82e5\u4f1a\u53d1\u751f\u9664\u6570\u4e3a\u96f6\u7684\u60c5\u51b5\uff0c\u5219\u9700\u8981\u901a\u8fc7\u4ee3\u7801\u907f\u514d\u8be5\u60c5\u51b5\u7684\u53d1\u751f\uff01<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">RuntimeException \u5f02\u5e38\u4f1a\u7531 Java \u865a\u62df\u673a\u81ea\u52a8\u629b\u51fa\u5e76\u81ea\u52a8\u6355\u83b7\uff08<strong>\u300c\u5c31\u7b97\u6211\u4eec\u6ca1\u5199\u5f02\u5e38\u6355\u83b7\u8bed\u53e5\u8fd0\u884c\u65f6\u4e5f\u4f1a\u629b\u51fa\u9519\u8bef\u300d<\/strong>\uff01\uff01\uff09\uff0c\u6b64\u7c7b\u5f02\u5e38\u7684\u51fa\u73b0\u7edd\u5927\u6570\u60c5\u51b5\u662f\u4ee3\u7801\u672c\u8eab\u6709\u95ee\u9898\u5e94\u8be5\u4ece\u903b\u8f91\u4e0a\u53bb\u89e3\u51b3\u5e76\u6539\u8fdb\u4ee3\u7801\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7f16\u8bd1\u65f6\u5f02\u5e38<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u5b9a\u4e49\u300d<\/strong>: Exception \u4e2d\u9664 RuntimeException \u53ca\u5176\u5b50\u7c7b\u4e4b\u5916\u7684\u5f02\u5e38\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u7279\u70b9\u300d<\/strong>: Java \u7f16\u8bd1\u5668\u4f1a\u68c0\u67e5\u5b83\u3002\u5982\u679c\u7a0b\u5e8f\u4e2d\u51fa\u73b0\u6b64\u7c7b\u5f02\u5e38\uff0c\u6bd4\u5982 ClassNotFoundException\uff08\u6ca1\u6709\u627e\u5230\u6307\u5b9a\u7684\u7c7b\u5f02\u5e38\uff09\uff0cIOException\uff08IO\u6d41\u5f02\u5e38\uff09\uff0c\u8981\u4e48\u901a\u8fc7throws\u8fdb\u884c\u58f0\u660e\u629b\u51fa\uff0c\u8981\u4e48\u901a\u8fc7try-catch\u8fdb\u884c\u6355\u83b7\u5904\u7406\uff0c\u5426\u5219\u4e0d\u80fd\u901a\u8fc7\u7f16\u8bd1\u3002\u5728\u7a0b\u5e8f\u4e2d\uff0c\u901a\u5e38\u4e0d\u4f1a\u81ea\u5b9a\u4e49\u8be5\u7c7b\u5f02\u5e38\uff0c\u800c\u662f\u76f4\u63a5\u4f7f\u7528\u7cfb\u7edf\u63d0\u4f9b\u7684\u5f02\u5e38\u7c7b\u3002<strong>\u300c\u8be5\u5f02\u5e38\u6211\u4eec\u5fc5\u987b\u624b\u52a8\u5728\u4ee3\u7801\u91cc\u6dfb\u52a0\u6355\u83b7\u8bed\u53e5\u6765\u5904\u7406\u8be5\u5f02\u5e38\u300d<\/strong>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u53d7\u68c0\u5f02\u5e38\u4e0e\u975e\u53d7\u68c0\u5f02\u5e38<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Java \u7684\u6240\u6709\u5f02\u5e38\u53ef\u4ee5\u5206\u4e3a\u53d7\u68c0\u5f02\u5e38\uff08checked exception\uff09\u548c\u975e\u53d7\u68c0\u5f02\u5e38\uff08unchecked exception\uff09\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u53d7\u68c0\u5f02\u5e38<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u7f16\u8bd1\u5668\u8981\u6c42\u5fc5\u987b\u5904\u7406\u7684\u5f02\u5e38\u3002\u6b63\u786e\u7684\u7a0b\u5e8f\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u5bb9\u6613\u51fa\u73b0\u7684\u3001\u7b26\u5408\u9884\u671f\u7684\u5f02\u5e38\u60c5\u51b5\u3002\u4e00\u65e6\u53d1\u751f\u6b64\u7c7b\u5f02\u5e38\uff0c\u5c31\u5fc5\u987b\u91c7\u7528\u67d0\u79cd\u65b9\u5f0f\u8fdb\u884c\u5904\u7406\u3002<strong>\u300c\u9664 RuntimeException \u53ca\u5176\u5b50\u7c7b\u5916\uff0c\u5176\u4ed6\u7684 Exception \u5f02\u5e38\u90fd\u5c5e\u4e8e\u53d7\u68c0\u5f02\u5e38\u300d<\/strong>\u3002\u7f16\u8bd1\u5668\u4f1a\u68c0\u67e5\u6b64\u7c7b\u5f02\u5e38\uff0c\u4e5f\u5c31\u662f\u8bf4\u5f53\u7f16\u8bd1\u5668\u68c0\u67e5\u5230\u5e94\u7528\u4e2d\u7684\u67d0\u5904\u53ef\u80fd\u4f1a\u6b64\u7c7b\u5f02\u5e38\u65f6\uff0c\u5c06\u4f1a\u63d0\u793a\u4f60\u5904\u7406\u672c\u5f02\u5e38\u2014\u2014\u8981\u4e48\u4f7f\u7528try-catch\u6355\u83b7\uff0c\u8981\u4e48\u4f7f\u7528\u65b9\u6cd5\u7b7e\u540d\u4e2d\u7528 throws \u5173\u952e\u5b57\u629b\u51fa\uff0c\u5426\u5219\u7f16\u8bd1\u4e0d\u901a\u8fc7\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u975e\u53d7\u68c0\u5f02\u5e38<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u7f16\u8bd1\u5668\u4e0d\u4f1a\u8fdb\u884c\u68c0\u67e5\u5e76\u4e14\u4e0d\u8981\u6c42\u5fc5\u987b\u5904\u7406\u7684\u5f02\u5e38\uff0c\u4e5f\u5c31\u8bf4\u5f53\u7a0b\u5e8f\u4e2d\u51fa\u73b0\u6b64\u7c7b\u5f02\u5e38\u65f6\uff0c\u5373\u4f7f\u6211\u4eec\u6ca1\u6709try-catch\u6355\u83b7\u5b83\uff0c\u4e5f\u6ca1\u6709\u4f7f\u7528throws\u629b\u51fa\u8be5\u5f02\u5e38\uff0c\u7f16\u8bd1\u4e5f\u4f1a\u6b63\u5e38\u901a\u8fc7\u3002<strong>\u300c\u8be5\u7c7b\u5f02\u5e38\u5305\u62ec\u8fd0\u884c\u65f6\u5f02\u5e38\uff08RuntimeException\u6781\u5176\u5b50\u7c7b\uff09\u548c\u9519\u8bef\uff08Error\uff09\u3002\u300d<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Java\u5f02\u5e38\u5173\u952e\u5b57<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u2022 <strong>\u300ctry\u300d<\/strong> \u2013 \u7528\u4e8e\u76d1\u542c\u3002\u5c06\u8981\u88ab\u76d1\u542c\u7684\u4ee3\u7801(\u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801)\u653e\u5728try\u8bed\u53e5\u5757\u4e4b\u5185\uff0c\u5f53try\u8bed\u53e5\u5757\u5185\u53d1\u751f\u5f02\u5e38\u65f6\uff0c\u5f02\u5e38\u5c31\u88ab\u629b\u51fa\u3002<br>\n\u2022 <strong>\u300ccatch\u300d<\/strong> \u2013 \u7528\u4e8e\u6355\u83b7\u5f02\u5e38\u3002catch\u7528\u6765\u6355\u83b7try\u8bed\u53e5\u5757\u4e2d\u53d1\u751f\u7684\u5f02\u5e38\u3002<br>\n\u2022 <strong>\u300cfinally\u300d<\/strong> \u2013 finally\u8bed\u53e5\u5757\u603b\u662f\u4f1a\u88ab\u6267\u884c\u3002\u5b83\u4e3b\u8981\u7528\u4e8e\u56de\u6536\u5728try\u5757\u91cc\u6253\u5f00\u7684\u7269\u529b\u8d44\u6e90(\u5982\u6570\u636e\u5e93\u8fde\u63a5\u3001\u7f51\u7edc\u8fde\u63a5\u548c\u78c1\u76d8\u6587\u4ef6)\u3002\u53ea\u6709finally\u5757\uff0c\u6267\u884c\u5b8c\u6210\u4e4b\u540e\uff0c\u624d\u4f1a\u56de\u6765\u6267\u884ctry\u6216\u8005catch\u5757\u4e2d\u7684return\u6216\u8005throw\u8bed\u53e5\uff0c\u5982\u679cfinally\u4e2d\u4f7f\u7528\u4e86return\u6216\u8005throw\u7b49\u7ec8\u6b62\u65b9\u6cd5\u7684\u8bed\u53e5\uff0c\u5219\u5c31\u4e0d\u4f1a\u8df3\u56de\u6267\u884c\uff0c\u76f4\u63a5\u505c\u6b62\u3002<br>\n\u2022 <strong>\u300cthrow\u300d<\/strong> \u2013 \u7528\u4e8e\u629b\u51fa\u5f02\u5e38\u3002<br>\n\u2022 <strong>\u300cthrows\u300d<\/strong> \u2013 \u7528\u5728\u65b9\u6cd5\u7b7e\u540d\u4e2d\uff0c\u7528\u4e8e\u58f0\u660e\u8be5\u65b9\u6cd5\u53ef\u80fd\u629b\u51fa\u7684\u5f02\u5e38\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Java\u5f02\u5e38\u5904\u7406<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2021\/12\/err1.png\" alt=\"\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Java \u901a\u8fc7\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u6cd5\u8fdb\u884c\u5f02\u5e38\u5904\u7406\uff0c\u4e00\u65e6\u65b9\u6cd5\u629b\u51fa\u5f02\u5e38\uff0c\u7cfb\u7edf\u81ea\u52a8\u6839\u636e\u8be5\u5f02\u5e38\u5bf9\u8c61\u5bfb\u627e\u5408\u9002\u5f02\u5e38\u5904\u7406\u5668\uff08Exception Handler\uff09\u6765\u5904\u7406\u8be5\u5f02\u5e38\uff0c\u628a\u5404\u79cd\u4e0d\u540c\u7684\u5f02\u5e38\u8fdb\u884c\u5206\u7c7b\uff0c\u5e76\u63d0\u4f9b\u4e86\u826f\u597d\u7684\u63a5\u53e3\u3002\u5728 Java \u4e2d\uff0c\u6bcf\u4e2a\u5f02\u5e38\u90fd\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5b83\u662f Throwable \u7c7b\u6216\u5176\u5b50\u7c7b\u7684\u5b9e\u4f8b\u3002\u5f53\u4e00\u4e2a\u65b9\u6cd5\u51fa\u73b0\u5f02\u5e38\u540e\u4fbf\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u4e2d\u5305\u542b\u6709\u5f02\u5e38\u4fe1\u606f\uff0c\u8c03\u7528\u8fd9\u4e2a\u5bf9\u8c61\u7684\u65b9\u6cd5\u53ef\u4ee5\u6355\u83b7\u5230\u8fd9\u4e2a\u5f02\u5e38\u5e76\u53ef\u4ee5\u5bf9\u5176\u8fdb\u884c\u5904\u7406\u3002Java \u7684\u5f02\u5e38\u5904\u7406\u662f\u901a\u8fc7 5 \u4e2a\u5173\u952e\u8bcd\u6765\u5b9e\u73b0\u7684\uff1atry\u3001catch\u3001throw\u3001throws \u548c finally\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728Java\u5e94\u7528\u4e2d\uff0c\u5f02\u5e38\u7684\u5904\u7406\u673a\u5236\u5206\u4e3a\u58f0\u660e\u5f02\u5e38\uff0c\u629b\u51fa\u5f02\u5e38\u548c\u6355\u83b7\u5f02\u5e38\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u58f0\u660e\u5f02\u5e38<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u5e38\uff0c\u5e94\u8be5\u6355\u83b7\u90a3\u4e9b\u77e5\u9053\u5982\u4f55\u5904\u7406\u7684\u5f02\u5e38\uff0c\u5c06\u4e0d\u77e5\u9053\u5982\u4f55\u5904\u7406\u7684\u5f02\u5e38\u7ee7\u7eed\u4f20\u9012\u4e0b\u53bb\u3002\u4f20\u9012\u5f02\u5e38\u53ef\u4ee5\u5728\u65b9\u6cd5\u7b7e\u540d\u5904\u4f7f\u7528 <strong>\u300cthrows\u300d<\/strong> \u5173\u952e\u5b57\u58f0\u660e\u53ef\u80fd\u4f1a\u629b\u51fa\u7684\u5f02\u5e38\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6ce8\u610f<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u975e\u68c0\u67e5\u5f02\u5e38\uff08Error\u3001RuntimeException \u6216\u5b83\u4eec\u7684\u5b50\u7c7b\uff09\u4e0d\u53ef\u4f7f\u7528 throws \u5173\u952e\u5b57\u6765\u58f0\u660e\u8981\u629b\u51fa\u7684\u5f02\u5e38\u3002<\/li><li>\u4e00\u4e2a\u65b9\u6cd5\u51fa\u73b0\u7f16\u8bd1\u65f6\u5f02\u5e38\uff0c\u5c31\u9700\u8981 try-catch\/ throws \u5904\u7406\uff0c\u5426\u5219\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u629b\u51fa\u5f02\u5e38<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u89c9\u5f97\u89e3\u51b3\u4e0d\u4e86\u67d0\u4e9b\u5f02\u5e38\u95ee\u9898\uff0c\u4e14\u4e0d\u9700\u8981\u8c03\u7528\u8005\u5904\u7406\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">throw\u5173\u952e\u5b57\u4f5c\u7528\u662f\u5728\u65b9\u6cd5\u5185\u90e8\u629b\u51fa\u4e00\u4e2a<code>Throwable<\/code>\u7c7b\u578b\u7684\u5f02\u5e38\u3002\u4efb\u4f55Java\u4ee3\u7801\u90fd\u53ef\u4ee5\u901a\u8fc7throw\u8bed\u53e5\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6355\u83b7\u5f02\u5e38<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u7a0b\u5e8f\u901a\u5e38\u5728\u8fd0\u884c\u4e4b\u524d\u4e0d\u62a5\u9519\uff0c\u4f46\u662f\u8fd0\u884c\u540e\u53ef\u80fd\u4f1a\u51fa\u73b0\u67d0\u4e9b\u672a\u77e5\u7684\u9519\u8bef\uff0c\u4f46\u662f\u8fd8\u4e0d\u60f3\u76f4\u63a5\u629b\u51fa\u5230\u4e0a\u4e00\u7ea7\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u901a\u8fc7try\u2026catch\u2026\u7684\u5f62\u5f0f\u8fdb\u884c\u5f02\u5e38\u6355\u83b7\uff0c\u4e4b\u540e\u6839\u636e\u4e0d\u540c\u7684\u5f02\u5e38\u60c5\u51b5\u6765\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5982\u4f55\u9009\u62e9\u5f02\u5e38\u7c7b\u578b<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u6839\u636e\u4e0b\u56fe\u6765\u9009\u62e9\u662f\u6355\u83b7\u5f02\u5e38\uff0c\u58f0\u660e\u5f02\u5e38\u8fd8\u662f\u629b\u51fa\u5f02\u5e38<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2021\/12\/err2.png\" alt=\"\u6355\u83b7\u5f02\u5e38\" \/><figcaption>\u6355\u83b7\u5f02\u5e38<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u5e38\u89c1\u5f02\u5e38\u5904\u7406\u65b9\u5f0f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u76f4\u63a5\u629b\u51fa\u5f02\u5e38<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u5e38\uff0c\u5e94\u8be5\u6355\u83b7\u90a3\u4e9b\u77e5\u9053\u5982\u4f55\u5904\u7406\u7684\u5f02\u5e38\uff0c\u5c06\u4e0d\u77e5\u9053\u5982\u4f55\u5904\u7406\u7684\u5f02\u5e38\u7ee7\u7eed\u4f20\u9012\u4e0b\u53bb\u3002\u4f20\u9012\u5f02\u5e38\u53ef\u4ee5\u5728\u65b9\u6cd5\u7b7e\u540d\u5904\u4f7f\u7528 <strong>\u300cthrows\u300d<\/strong> \u5173\u952e\u5b57\u58f0\u660e\u53ef\u80fd\u4f1a\u629b\u51fa\u7684\u5f02\u5e38\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private static void readFile(String filePath) throws IOException {\n    File file = new File(filePath);\n    String result;\n    BufferedReader reader = new BufferedReader(new FileReader(file));\n    while((result = reader.readLine())!=null) {\n        System.out.println(result);\n    }\n    reader.close();\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5c01\u88c5\u5f02\u5e38\u518d\u629b\u51fa<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u65f6\u6211\u4eec\u4f1a\u4ece catch \u4e2d\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\uff0c\u76ee\u7684\u662f\u4e3a\u4e86\u6539\u53d8\u5f02\u5e38\u7684\u7c7b\u578b\u3002\u591a\u7528\u4e8e\u5728\u591a\u7cfb\u7edf\u96c6\u6210\u65f6\uff0c\u5f53\u67d0\u4e2a\u5b50\u7cfb\u7edf\u6545\u969c\uff0c\u5f02\u5e38\u7c7b\u578b\u53ef\u80fd\u6709\u591a\u79cd\uff0c\u53ef\u4ee5\u7528\u7edf\u4e00\u7684\u5f02\u5e38\u7c7b\u578b\u5411\u5916\u66b4\u9732\uff0c\u4e0d\u9700\u66b4\u9732\u592a\u591a\u5185\u90e8\u5f02\u5e38\u7ec6\u8282\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private static void readFile(String filePath) throws MyException {    \n    try {\n        \/\/ code\n    } catch (IOException e) {\n        MyException ex = new MyException(\"read file failed.\");\n        ex.initCause(e);\n        throw ex;\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6355\u83b7\u5f02\u5e38<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4e00\u4e2a try-catch \u8bed\u53e5\u5757\u4e2d\u53ef\u4ee5\u6355\u83b7\u591a\u4e2a\u5f02\u5e38\u7c7b\u578b\uff0c\u5e76\u5bf9\u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38\u505a\u51fa\u4e0d\u540c\u7684\u5904\u7406<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private static void readFile(String filePath) {\n    try {\n        \/\/ code\n    } catch (FileNotFoundException e) {\n        \/\/ handle FileNotFoundException\n    } catch (IOException e){\n        \/\/ handle IOException\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u540c\u4e00\u4e2a catch \u4e5f\u53ef\u4ee5\u6355\u83b7\u591a\u79cd\u7c7b\u578b\u5f02\u5e38\uff0c\u7528 | \u9694\u5f00<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private static void readFile(String filePath) {\n    try {\n        \/\/ code\n    } catch (FileNotFoundException | UnknownHostException e) {\n        \/\/ handle FileNotFoundException or UnknownHostException\n    } catch (IOException e){\n        \/\/ handle IOException\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u81ea\u5b9a\u4e49\u5f02\u5e38<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e60\u60ef\u4e0a\uff0c\u5b9a\u4e49\u4e00\u4e2a\u5f02\u5e38\u7c7b\u5e94\u5305\u542b\u4e24\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u4e00\u4e2a\u65e0\u53c2\u6784\u9020\u51fd\u6570\u548c\u4e00\u4e2a\u5e26\u6709\u8be6\u7ec6\u63cf\u8ff0\u4fe1\u606f\u7684\u6784\u9020\u51fd\u6570\uff08Throwable \u7684 toString \u65b9\u6cd5\u4f1a\u6253\u5370\u8fd9\u4e9b\u8be6\u7ec6\u4fe1\u606f\uff0c\u8c03\u8bd5\u65f6\u5f88\u6709\u7528\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class MyException extends Exception {\n    public MyException(){ }\n    public MyException(String msg){\n        super(msg);\n    }\n    \/\/ ...\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">try-catch-finally<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u65b9\u6cd5\u4e2d\u53d1\u751f\u5f02\u5e38\uff0c\u5f02\u5e38\u5904\u4e4b\u540e\u7684\u4ee3\u7801\u4e0d\u4f1a\u518d\u6267\u884c\uff0c\u5982\u679c\u4e4b\u524d\u83b7\u53d6\u4e86\u4e00\u4e9b\u672c\u5730\u8d44\u6e90\u9700\u8981\u91ca\u653e\uff0c\u5219\u9700\u8981\u5728\u65b9\u6cd5\u6b63\u5e38\u7ed3\u675f\u65f6\u548c catch \u8bed\u53e5\u4e2d\u90fd\u8c03\u7528\u91ca\u653e\u672c\u5730\u8d44\u6e90\u7684\u4ee3\u7801\uff0c\u663e\u5f97\u4ee3\u7801\u6bd4\u8f83\u7e41\u7410\uff0cfinally \u8bed\u53e5\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private static void readFile(String filePath) throws MyException {\n    File file = new File(filePath);\n    String result;\n    BufferedReader reader = null;\n    try {\n        reader = new BufferedReader(new FileReader(file));\n        while((result = reader.readLine())!=null) {\n            System.out.println(result);\n        }\n    } catch (IOException e) {\n        System.out.println(\"readFile method catch block.\");\n        MyException ex = new MyException(\"read file failed.\");\n        ex.initCause(e);\n        throw ex;\n    } finally {\n        System.out.println(\"readFile method finally block.\");\n        if (null != reader) {\n            try {\n                reader.close();\n            } catch (IOException e) {\n                e.printStackTrace();\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8c03\u7528\u8be5\u65b9\u6cd5\u65f6\uff0c\u8bfb\u53d6\u6587\u4ef6\u65f6\u82e5\u53d1\u751f\u5f02\u5e38\uff0c\u4ee3\u7801\u4f1a\u8fdb\u5165 catch \u4ee3\u7801\u5757\uff0c\u4e4b\u540e\u8fdb\u5165 finally \u4ee3\u7801\u5757\uff1b\u82e5\u8bfb\u53d6\u6587\u4ef6\u65f6\u672a\u53d1\u751f\u5f02\u5e38\uff0c\u5219\u4f1a\u8df3\u8fc7 catch \u4ee3\u7801\u5757\u76f4\u63a5\u8fdb\u5165 finally \u4ee3\u7801\u5757\u3002\u6240\u4ee5\u65e0\u8bba\u4ee3\u7801\u4e2d\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0cfianlly \u4e2d\u7684\u4ee3\u7801\u90fd\u4f1a\u6267\u884c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u82e5 catch \u4ee3\u7801\u5757\u4e2d\u5305\u542b return \u8bed\u53e5\uff0cfinally \u4e2d\u7684\u4ee3\u7801\u8fd8\u4f1a\u6267\u884c\u5417\uff1f\u5c06\u4ee5\u4e0a\u4ee3\u7801\u4e2d\u7684 catch \u5b50\u53e5\u4fee\u6539\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>catch (IOException e) {\n    System.out.println(\"readFile method catch block.\");\n    return;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8c03\u7528 readFile \u65b9\u6cd5\uff0c\u89c2\u5bdf\u5f53 catch \u5b50\u53e5\u4e2d\u8c03\u7528 return \u8bed\u53e5\u65f6\uff0cfinally \u5b50\u53e5\u662f\u5426\u6267\u884c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>readFile method catch block.\nreadFile method finally block.<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u89c1\uff0c\u5373\u4f7f catch \u4e2d\u5305\u542b\u4e86 return \u8bed\u53e5\uff0cfinally \u5b50\u53e5\u4f9d\u7136\u4f1a\u6267\u884c\u3002\u82e5 finally \u4e2d\u4e5f\u5305\u542b return \u8bed\u53e5\uff0cfinally \u4e2d\u7684 return \u4f1a\u8986\u76d6\u524d\u9762\u7684 return.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">try-with-resource<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u4f8b\u5b50\u4e2d\uff0cfinally \u4e2d\u7684 close \u65b9\u6cd5\u4e5f\u53ef\u80fd\u629b\u51fa IOException, \u4ece\u800c\u8986\u76d6\u4e86\u539f\u59cb\u5f02\u5e38\u3002JAVA 7 \u63d0\u4f9b\u4e86\u66f4\u4f18\u96c5\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u8d44\u6e90\u7684\u81ea\u52a8\u91ca\u653e\uff0c\u81ea\u52a8\u91ca\u653e\u7684\u8d44\u6e90\u9700\u8981\u662f\u5b9e\u73b0\u4e86 AutoCloseable \u63a5\u53e3\u7684\u7c7b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private  static void tryWithResourceTest(){\n    try (Scanner scanner = new Scanner(new FileInputStream(\"c:\/abc\"),\"UTF-8\")){\n        \/\/ code\n    } catch (IOException e){\n        \/\/ handle exception\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">try \u4ee3\u7801\u5757\u9000\u51fa\u65f6\uff0c\u4f1a\u81ea\u52a8\u8c03\u7528 scanner.close \u65b9\u6cd5\uff0c\u548c\u628a scanner.close \u65b9\u6cd5\u653e\u5728 finally \u4ee3\u7801\u5757\u4e2d\u4e0d\u540c\u7684\u662f\uff0c\u82e5 scanner.close \u629b\u51fa\u5f02\u5e38\uff0c\u5219\u4f1a\u88ab\u6291\u5236\uff0c\u629b\u51fa\u7684\u4ecd\u7136\u4e3a\u539f\u59cb\u5f02\u5e38\u3002\u88ab\u6291\u5236\u7684\u5f02\u5e38\u4f1a\u7531 addSusppressed \u65b9\u6cd5\u6dfb\u52a0\u5230\u539f\u6765\u7684\u5f02\u5e38\uff0c\u5982\u679c\u60f3\u8981\u83b7\u53d6\u88ab\u6291\u5236\u7684\u5f02\u5e38\u5217\u8868\uff0c\u53ef\u4ee5\u8c03\u7528 getSuppressed \u65b9\u6cd5\u6765\u83b7\u53d6\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Java\u5f02\u5e38\u5e38\u89c1\u9762\u8bd5\u9898<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Error \u548c Exception \u533a\u522b\u662f\u4ec0\u4e48\uff1f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Error \u7c7b\u578b\u7684\u9519\u8bef\u901a\u5e38\u4e3a\u865a\u62df\u673a\u76f8\u5173\u9519\u8bef\uff0c\u5982\u7cfb\u7edf\u5d29\u6e83\uff0c\u5185\u5b58\u4e0d\u8db3\uff0c\u5806\u6808\u6ea2\u51fa\u7b49\uff0c\u7f16\u8bd1\u5668\u4e0d\u4f1a\u5bf9\u8fd9\u7c7b\u9519\u8bef\u8fdb\u884c\u68c0\u6d4b\uff0cJAVA \u5e94\u7528\u7a0b\u5e8f\u4e5f\u4e0d\u5e94\u5bf9\u8fd9\u7c7b\u9519\u8bef\u8fdb\u884c\u6355\u83b7\uff0c\u4e00\u65e6\u8fd9\u7c7b\u9519\u8bef\u53d1\u751f\uff0c\u901a\u5e38\u5e94\u7528\u7a0b\u5e8f\u4f1a\u88ab\u7ec8\u6b62\uff0c\u4ec5\u9760\u5e94\u7528\u7a0b\u5e8f\u672c\u8eab\u65e0\u6cd5\u6062\u590d\uff1b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Exception \u7c7b\u7684\u9519\u8bef\u662f\u53ef\u4ee5\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u8fdb\u884c\u6355\u83b7\u5e76\u5904\u7406\u7684\uff0c\u901a\u5e38\u9047\u5230\u8fd9\u79cd\u9519\u8bef\uff0c\u5e94\u5bf9\u5176\u8fdb\u884c\u5904\u7406\uff0c\u4f7f\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u7ee7\u7eed\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u8fd0\u884c\u65f6\u5f02\u5e38\u548c\u4e00\u822c\u5f02\u5e38(\u53d7\u68c0\u5f02\u5e38)\u533a\u522b\u662f\u4ec0\u4e48\uff1f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u65f6\u5f02\u5e38\u5305\u62ec RuntimeException \u7c7b\u53ca\u5176\u5b50\u7c7b\uff0c\u8868\u793a JVM \u5728\u8fd0\u884c\u671f\u95f4\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u3002 Java \u7f16\u8bd1\u5668\u4e0d\u4f1a\u68c0\u67e5\u8fd0\u884c\u65f6\u5f02\u5e38\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53d7\u68c0\u5f02\u5e38\u662fException \u4e2d\u9664 RuntimeException \u53ca\u5176\u5b50\u7c7b\u4e4b\u5916\u7684\u5f02\u5e38\u3002 Java \u7f16\u8bd1\u5668\u4f1a\u68c0\u67e5\u53d7\u68c0\u5f02\u5e38\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300cRuntimeException\u5f02\u5e38\u548c\u53d7\u68c0\u5f02\u5e38\u4e4b\u95f4\u7684\u533a\u522b\u300d<\/strong>\uff1a\u662f\u5426\u5f3a\u5236\u8981\u6c42\u8c03\u7528\u8005\u5fc5\u987b\u5904\u7406\u6b64\u5f02\u5e38\uff0c\u5982\u679c\u5f3a\u5236\u8981\u6c42\u8c03\u7528\u8005\u5fc5\u987b\u8fdb\u884c\u5904\u7406\uff0c\u90a3\u4e48\u5c31\u4f7f\u7528\u53d7\u68c0\u5f02\u5e38\uff0c\u5426\u5219\u5c31\u9009\u62e9\u975e\u53d7\u68c0\u5f02\u5e38(RuntimeException)\u3002\u4e00\u822c\u6765\u8bb2\uff0c\u5982\u679c\u6ca1\u6709\u7279\u6b8a\u7684\u8981\u6c42\uff0c\u6211\u4eec\u5efa\u8bae\u4f7f\u7528RuntimeException\u5f02\u5e38\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. JVM \u662f\u5982\u4f55\u5904\u7406\u5f02\u5e38\u7684\uff1f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4e00\u4e2a\u65b9\u6cd5\u4e2d\u5982\u679c\u53d1\u751f\u5f02\u5e38\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u521b\u5efa\u4e00\u4e2a\u5f02\u5e38\u5bf9\u8c61\uff0c\u5e76\u8f6c\u4ea4\u7ed9 JVM\uff0c\u8be5\u5f02\u5e38\u5bf9\u8c61\u5305\u542b\u5f02\u5e38\u540d\u79f0\uff0c\u5f02\u5e38\u63cf\u8ff0\u4ee5\u53ca\u5f02\u5e38\u53d1\u751f\u65f6\u5e94\u7528\u7a0b\u5e8f\u7684\u72b6\u6001\u3002\u521b\u5efa\u5f02\u5e38\u5bf9\u8c61\u5e76\u8f6c\u4ea4\u7ed9 JVM \u7684\u8fc7\u7a0b\u79f0\u4e3a\u629b\u51fa\u5f02\u5e38\u3002\u53ef\u80fd\u6709\u4e00\u7cfb\u5217\u7684\u65b9\u6cd5\u8c03\u7528\uff0c\u6700\u7ec8\u624d\u8fdb\u5165\u629b\u51fa\u5f02\u5e38\u7684\u65b9\u6cd5\uff0c\u8fd9\u4e00\u7cfb\u5217\u65b9\u6cd5\u8c03\u7528\u7684\u6709\u5e8f\u5217\u8868\u53eb\u505a\u8c03\u7528\u6808\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">JVM \u4f1a\u987a\u7740\u8c03\u7528\u6808\u53bb\u67e5\u627e\u770b\u662f\u5426\u6709\u53ef\u4ee5\u5904\u7406\u5f02\u5e38\u7684\u4ee3\u7801\uff0c\u5982\u679c\u6709\uff0c\u5219\u8c03\u7528\u5f02\u5e38\u5904\u7406\u4ee3\u7801\u3002\u5f53 JVM \u53d1\u73b0\u53ef\u4ee5\u5904\u7406\u5f02\u5e38\u7684\u4ee3\u7801\u65f6\uff0c\u4f1a\u628a\u53d1\u751f\u7684\u5f02\u5e38\u4f20\u9012\u7ed9\u5b83\u3002\u5982\u679c JVM \u6ca1\u6709\u627e\u5230\u53ef\u4ee5\u5904\u7406\u8be5\u5f02\u5e38\u7684\u4ee3\u7801\u5757\uff0cJVM \u5c31\u4f1a\u5c06\u8be5\u5f02\u5e38\u8f6c\u4ea4\u7ed9\u9ed8\u8ba4\u7684\u5f02\u5e38\u5904\u7406\u5668\uff08\u9ed8\u8ba4\u5904\u7406\u5668\u4e3a JVM \u7684\u4e00\u90e8\u5206\uff09\uff0c\u9ed8\u8ba4\u5f02\u5e38\u5904\u7406\u5668\u6253\u5370\u51fa\u5f02\u5e38\u4fe1\u606f\u5e76\u7ec8\u6b62\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. throw \u548c throws \u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Java \u4e2d\u7684\u5f02\u5e38\u5904\u7406\u9664\u4e86\u5305\u62ec\u6355\u83b7\u5f02\u5e38\u548c\u5904\u7406\u5f02\u5e38\u4e4b\u5916\uff0c\u8fd8\u5305\u62ec\u58f0\u660e\u5f02\u5e38\u548c\u62cb\u51fa\u5f02\u5e38\uff0c\u53ef\u4ee5\u901a\u8fc7 throws \u5173\u952e\u5b57\u5728\u65b9\u6cd5\u4e0a\u58f0\u660e\u8be5\u65b9\u6cd5\u8981\u62cb\u51fa\u7684\u5f02\u5e38\uff0c\u6216\u8005\u5728\u65b9\u6cd5\u5185\u90e8\u901a\u8fc7 throw \u62cb\u51fa\u5f02\u5e38\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300cthrows \u5173\u952e\u5b57\u548c throw \u5173\u952e\u5b57\u5728\u4f7f\u7528\u4e0a\u7684\u51e0\u70b9\u533a\u522b\u5982\u4e0b\u300d<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>throw \u5173\u952e\u5b57\u7528\u5728\u65b9\u6cd5\u5185\u90e8\uff0c\u53ea\u80fd\u7528\u4e8e\u629b\u51fa\u4e00\u79cd\u5f02\u5e38\uff0c\u7528\u6765\u629b\u51fa\u65b9\u6cd5\u6216\u4ee3\u7801\u5757\u4e2d\u7684\u5f02\u5e38\uff0c\u53d7\u67e5\u5f02\u5e38\u548c\u975e\u53d7\u67e5\u5f02\u5e38\u90fd\u53ef\u4ee5\u88ab\u629b\u51fa\u3002<\/li><li>throws \u5173\u952e\u5b57\u7528\u5728\u65b9\u6cd5\u58f0\u660e\u4e0a\uff0c\u53ef\u4ee5\u629b\u51fa\u591a\u4e2a\u5f02\u5e38\uff0c\u7528\u6765\u6807\u8bc6\u8be5\u65b9\u6cd5\u53ef\u80fd\u629b\u51fa\u7684\u5f02\u5e38\u5217\u8868\u3002\u4e00\u4e2a\u65b9\u6cd5\u7528 throws \u6807\u8bc6\u4e86\u53ef\u80fd\u629b\u51fa\u7684\u5f02\u5e38\u5217\u8868\uff0c\u8c03\u7528\u8be5\u65b9\u6cd5\u7684\u65b9\u6cd5\u4e2d\u5fc5\u987b\u5305\u542b\u53ef\u5904\u7406\u5f02\u5e38\u7684\u4ee3\u7801\uff0c\u5426\u5219\u4e5f\u8981\u5728\u65b9\u6cd5\u7b7e\u540d\u4e2d\u7528 throws \u5173\u952e\u5b57\u58f0\u660e\u76f8\u5e94\u7684\u5f02\u5e38\u3002<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">5. final\u3001finally\u3001finalize \u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>final\u53ef\u4ee5\u4fee\u9970\u7c7b\u3001\u53d8\u91cf\u3001\u65b9\u6cd5\uff0c\u4fee\u9970\u7c7b\u8868\u793a\u8be5\u7c7b\u4e0d\u80fd\u88ab\u7ee7\u627f\u3001\u4fee\u9970\u65b9\u6cd5\u8868\u793a\u8be5\u65b9\u6cd5\u4e0d\u80fd\u88ab\u91cd\u5199\u3001\u4fee\u9970\u53d8\u91cf\u8868\u793a\u8be5\u53d8\u91cf\u662f\u4e00\u4e2a\u5e38\u91cf\u4e0d\u80fd\u88ab\u91cd\u65b0\u8d4b\u503c\u3002<\/li><li>finally\u4e00\u822c\u4f5c\u7528\u5728try-catch\u4ee3\u7801\u5757\u4e2d\uff0c\u5728\u5904\u7406\u5f02\u5e38\u7684\u65f6\u5019\uff0c\u901a\u5e38\u6211\u4eec\u5c06\u4e00\u5b9a\u8981\u6267\u884c\u7684\u4ee3\u7801\u65b9\u6cd5finally\u4ee3\u7801\u5757\u4e2d\uff0c\u8868\u793a\u4e0d\u7ba1\u662f\u5426\u51fa\u73b0\u5f02\u5e38\uff0c\u8be5\u4ee3\u7801\u5757\u90fd\u4f1a\u6267\u884c\uff0c\u4e00\u822c\u7528\u6765\u5b58\u653e\u4e00\u4e9b\u5173\u95ed\u8d44\u6e90\u7684\u4ee3\u7801\u3002<\/li><li>finalize\u662f\u4e00\u4e2a\u65b9\u6cd5\uff0c\u5c5e\u4e8eObject\u7c7b\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u800cObject\u7c7b\u662f\u6240\u6709\u7c7b\u7684\u7236\u7c7b\uff0cJava \u4e2d\u5141\u8bb8\u4f7f\u7528 finalize()\u65b9\u6cd5\u5728\u5783\u573e\u6536\u96c6\u5668\u5c06\u5bf9\u8c61\u4ece\u5185\u5b58\u4e2d\u6e05\u9664\u51fa\u53bb\u4e4b\u524d\u505a\u5fc5\u8981\u7684\u6e05\u7406\u5de5\u4f5c\u3002<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">6. NoClassDefFoundError \u548c ClassNotFoundException \u533a\u522b\uff1f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">NoClassDefFoundError \u662f\u4e00\u4e2a Error \u7c7b\u578b\u7684\u5f02\u5e38\uff0c\u662f\u7531 JVM \u5f15\u8d77\u7684\uff0c\u4e0d\u5e94\u8be5\u5c1d\u8bd5\u6355\u83b7\u8fd9\u4e2a\u5f02\u5e38\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f15\u8d77\u8be5\u5f02\u5e38\u7684\u539f\u56e0\u662f JVM \u6216 ClassLoader \u5c1d\u8bd5\u52a0\u8f7d\u67d0\u7c7b\u65f6\u5728\u5185\u5b58\u4e2d\u627e\u4e0d\u5230\u8be5\u7c7b\u7684\u5b9a\u4e49\uff0c\u8be5\u52a8\u4f5c\u53d1\u751f\u5728\u8fd0\u884c\u671f\u95f4\uff0c\u5373\u7f16\u8bd1\u65f6\u8be5\u7c7b\u5b58\u5728\uff0c\u4f46\u662f\u5728\u8fd0\u884c\u65f6\u5374\u627e\u4e0d\u5230\u4e86\uff0c\u53ef\u80fd\u662f\u53d8\u5f02\u540e\u88ab\u5220\u9664\u4e86\u7b49\u539f\u56e0\u5bfc\u81f4\uff1b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">ClassNotFoundException \u662f\u4e00\u4e2a\u53d7\u67e5\u5f02\u5e38\uff0c\u9700\u8981\u663e\u5f0f\u5730\u4f7f\u7528 try-catch \u5bf9\u5176\u8fdb\u884c\u6355\u83b7\u548c\u5904\u7406\uff0c\u6216\u5728\u65b9\u6cd5\u7b7e\u540d\u4e2d\u7528 throws \u5173\u952e\u5b57\u8fdb\u884c\u58f0\u660e\u3002\u5f53\u4f7f\u7528 Class.forName, ClassLoader.loadClass \u6216 ClassLoader.findSystemClass \u52a8\u6001\u52a0\u8f7d\u7c7b\u5230\u5185\u5b58\u7684\u65f6\u5019\uff0c\u901a\u8fc7\u4f20\u5165\u7684\u7c7b\u8def\u5f84\u53c2\u6570\u6ca1\u6709\u627e\u5230\u8be5\u7c7b\uff0c\u5c31\u4f1a\u629b\u51fa\u8be5\u5f02\u5e38\uff1b\u53e6\u4e00\u79cd\u629b\u51fa\u8be5\u5f02\u5e38\u7684\u53ef\u80fd\u539f\u56e0\u662f\u67d0\u4e2a\u7c7b\u5df2\u7ecf\u7531\u4e00\u4e2a\u7c7b\u52a0\u8f7d\u5668\u52a0\u8f7d\u81f3\u5185\u5b58\u4e2d\uff0c\u53e6\u4e00\u4e2a\u52a0\u8f7d\u5668\u53c8\u5c1d\u8bd5\u53bb\u52a0\u8f7d\u5b83\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">7. try-catch-finally \u4e2d\u54ea\u4e2a\u90e8\u5206\u53ef\u4ee5\u7701\u7565\uff1f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b54\uff1acatch \u53ef\u4ee5\u7701\u7565<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u539f\u56e0\u300d<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u66f4\u4e3a\u4e25\u683c\u7684\u8bf4\u6cd5\u5176\u5b9e\u662f\uff1atry\u53ea\u9002\u5408\u5904\u7406\u8fd0\u884c\u65f6\u5f02\u5e38\uff0ctry+catch\u9002\u5408\u5904\u7406\u8fd0\u884c\u65f6\u5f02\u5e38+\u666e\u901a\u5f02\u5e38\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4f60\u53ea\u7528try\u53bb\u5904\u7406\u666e\u901a\u5f02\u5e38\u5374\u4e0d\u52a0\u4ee5catch\u5904\u7406\uff0c\u7f16\u8bd1\u662f\u901a\u4e0d\u8fc7\u7684\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u786c\u6027\u89c4\u5b9a\uff0c\u666e\u901a\u5f02\u5e38\u5982\u679c\u9009\u62e9\u6355\u83b7\uff0c\u5219\u5fc5\u987b\u7528catch\u663e\u793a\u58f0\u660e\u4ee5\u4fbf\u8fdb\u4e00\u6b65\u5904\u7406\u3002\u800c\u8fd0\u884c\u65f6\u5f02\u5e38\u5728\u7f16\u8bd1\u65f6\u6ca1\u6709\u5982\u6b64\u89c4\u5b9a\uff0c\u6240\u4ee5catch\u53ef\u4ee5\u7701\u7565\uff0c\u4f60\u52a0\u4e0acatch\u7f16\u8bd1\u5668\u4e5f\u89c9\u5f97\u65e0\u53ef\u539a\u975e\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7406\u8bba\u4e0a\uff0c\u7f16\u8bd1\u5668\u770b\u4efb\u4f55\u4ee3\u7801\u90fd\u4e0d\u987a\u773c\uff0c\u90fd\u89c9\u5f97\u53ef\u80fd\u6709\u6f5c\u5728\u7684\u95ee\u9898\uff0c\u6240\u4ee5\u4f60\u5373\u4f7f\u5bf9\u6240\u6709\u4ee3\u7801\u52a0\u4e0atry\uff0c\u4ee3\u7801\u5728\u8fd0\u884c\u671f\u65f6\u4e5f\u53ea\u4e0d\u8fc7\u662f\u5728\u6b63\u5e38\u8fd0\u884c\u7684\u57fa\u7840\u4e0a\u52a0\u4e00\u5c42\u76ae\u3002\u4f46\u662f\u4f60\u4e00\u65e6\u5bf9\u4e00\u6bb5\u4ee3\u7801\u52a0\u4e0atry\uff0c\u5c31\u7b49\u4e8e\u663e\u793a\u5730\u627f\u8bfa\u7f16\u8bd1\u5668\uff0c\u5bf9\u8fd9\u6bb5\u4ee3\u7801\u53ef\u80fd\u629b\u51fa\u7684\u5f02\u5e38\u8fdb\u884c\u6355\u83b7\u800c\u975e\u5411\u4e0a\u629b\u51fa\u5904\u7406\u3002\u5982\u679c\u662f\u666e\u901a\u5f02\u5e38\uff0c\u7f16\u8bd1\u5668\u8981\u6c42\u5fc5\u987b\u7528catch\u6355\u83b7\u4ee5\u4fbf\u8fdb\u4e00\u6b65\u5904\u7406\uff1b\u5982\u679c\u8fd0\u884c\u65f6\u5f02\u5e38\uff0c\u6355\u83b7\u7136\u540e\u4e22\u5f03\u5e76\u4e14+finally\u626b\u5c3e\u5904\u7406\uff0c\u6216\u8005\u52a0\u4e0acatch\u6355\u83b7\u4ee5\u4fbf\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u81f3\u4e8e\u52a0\u4e0afinally\uff0c\u5219\u662f\u5728\u4e0d\u7ba1\u6709\u6ca1\u6355\u83b7\u5f02\u5e38\uff0c\u90fd\u8981\u8fdb\u884c\u7684\u201c\u626b\u5c3e\u201d\u5904\u7406\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">8. try-catch-finally \u4e2d\uff0c\u5982\u679c catch \u4e2d return \u4e86\uff0cfinally \u8fd8\u4f1a\u6267\u884c\u5417\uff1f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b54\uff1a\u4f1a\u6267\u884c\uff0c\u5728 return \u524d\u6267\u884c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u6ce8\u610f\u300d<\/strong>\uff1a\u5728 finally \u4e2d\u6539\u53d8\u8fd4\u56de\u503c\u7684\u505a\u6cd5\u662f\u4e0d\u597d\u7684\uff0c\u56e0\u4e3a\u5982\u679c\u5b58\u5728 finally \u4ee3\u7801\u5757\uff0ctry\u4e2d\u7684 return \u8bed\u53e5\u4e0d\u4f1a\u7acb\u9a6c\u8fd4\u56de\u8c03\u7528\u8005\uff0c\u800c\u662f\u8bb0\u5f55\u4e0b\u8fd4\u56de\u503c\u5f85 finally \u4ee3\u7801\u5757\u6267\u884c\u5b8c\u6bd5\u4e4b\u540e\u518d\u5411\u8c03\u7528\u8005\u8fd4\u56de\u5176\u503c\uff0c\u7136\u540e\u5982\u679c\u5728 finally \u4e2d\u4fee\u6539\u4e86\u8fd4\u56de\u503c\uff0c\u5c31\u4f1a\u8fd4\u56de\u4fee\u6539\u540e\u7684\u503c\u3002\u663e\u7136\uff0c\u5728 finally \u4e2d\u8fd4\u56de\u6216\u8005\u4fee\u6539\u8fd4\u56de\u503c\u4f1a\u5bf9\u7a0b\u5e8f\u9020\u6210\u5f88\u5927\u7684\u56f0\u6270\uff0cC#\u4e2d\u76f4\u63a5\u7528\u7f16\u8bd1\u9519\u8bef\u7684\u65b9\u5f0f\u6765\u963b\u6b62\u7a0b\u5e8f\u5458\u5e72\u8fd9\u79cd\u9f8c\u9f8a\u7684\u4e8b\u60c5\uff0cJava \u4e2d\u4e5f\u53ef\u4ee5\u901a\u8fc7\u63d0\u5347\u7f16\u8bd1\u5668\u7684\u8bed\u6cd5\u68c0\u67e5\u7ea7\u522b\u6765\u4ea7\u751f\u8b66\u544a\u6216\u9519\u8bef\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u4ee3\u7801\u793a\u4f8b1\uff1a\u300d<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static int getInt() {\n    int a = 10;\n    try {\n        System.out.println(a \/ 0);\n        a = 20;\n    } catch (ArithmeticException e) {\n        a = 30;\n        return a;\n        \/*\n         * return a \u5728\u7a0b\u5e8f\u6267\u884c\u5230\u8fd9\u4e00\u6b65\u7684\u65f6\u5019\uff0c\u8fd9\u91cc\u4e0d\u662freturn a \u800c\u662f return 30\uff1b\u8fd9\u4e2a\u8fd4\u56de\u8def\u5f84\u5c31\u5f62\u6210\u4e86\n         * \u4f46\u662f\u5462\uff0c\u5b83\u53d1\u73b0\u540e\u9762\u8fd8\u6709finally\uff0c\u6240\u4ee5\u7ee7\u7eed\u6267\u884cfinally\u7684\u5185\u5bb9\uff0ca=40\n         * \u518d\u6b21\u56de\u5230\u4ee5\u524d\u7684\u8def\u5f84,\u7ee7\u7eed\u8d70return 30\uff0c\u5f62\u6210\u8fd4\u56de\u8def\u5f84\u4e4b\u540e\uff0c\u8fd9\u91cc\u7684a\u5c31\u4e0d\u662fa\u53d8\u91cf\u4e86\uff0c\u800c\u662f\u5e38\u91cf30\n         *\/\n    } finally {\n        a = 40;\n    }\n\treturn a;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6267\u884c\u7ed3\u679c\uff1a30<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u4ee3\u7801\u793a\u4f8b2\uff1a\u300d<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static int getInt() {\n    int a = 10;\n    try {\n        System.out.println(a \/ 0);\n        a = 20;\n    } catch (ArithmeticException e) {\n        a = 30;\n        return a;\n    } finally {\n        a = 40;\n        \/\/\u5982\u679c\u8fd9\u6837\uff0c\u5c31\u53c8\u91cd\u65b0\u5f62\u6210\u4e86\u4e00\u6761\u8fd4\u56de\u8def\u5f84\uff0c\u7531\u4e8e\u53ea\u80fd\u901a\u8fc71\u4e2areturn\u8fd4\u56de\uff0c\u6240\u4ee5\u8fd9\u91cc\u76f4\u63a5\u8fd4\u56de40\n        return a; \n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6267\u884c\u7ed3\u679c\uff1a40<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">9. \u7c7b ExampleA \u7ee7\u627f Exception\uff0c\u7c7b ExampleB \u7ee7\u627fExampleA\u3002<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u5982\u4e0b\u4ee3\u7801\u7247\u65ad\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n\tthrow new ExampleB(\"b\")\n} catch\uff08ExampleA e\uff09{\n\tSystem.out.println(\"ExampleA\");\n} catch\uff08Exception e\uff09{\n\tSystem.out.println(\"Exception\");\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bf7\u95ee\u6267\u884c\u6b64\u6bb5\u4ee3\u7801\u7684\u8f93\u51fa\u662f\u4ec0\u4e48\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u7b54\u300d<\/strong>\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8f93\u51fa\uff1aExampleA\u3002\uff08\u6839\u636e\u91cc\u6c0f\u4ee3\u6362\u539f\u5219[\u80fd\u4f7f\u7528\u7236\u7c7b\u578b\u7684\u5730\u65b9\u4e00\u5b9a\u80fd\u4f7f\u7528\u5b50\u7c7b\u578b]\uff0c\u6293\u53d6 ExampleA \u7c7b\u578b\u5f02\u5e38\u7684 catch \u5757\u80fd\u591f\u6293\u4f4f try \u5757\u4e2d\u629b\u51fa\u7684 ExampleB \u7c7b\u578b\u7684\u5f02\u5e38\uff09<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9762\u8bd5\u9898 &#8211; \u8bf4\u51fa\u4e0b\u9762\u4ee3\u7801\u7684\u8fd0\u884c\u7ed3\u679c\u3002\uff08\u6b64\u9898\u7684\u51fa\u5904\u662f\u300aJava \u7f16\u7a0b\u601d\u60f3\u300b\u4e00\u4e66\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Annoyance extends Exception {\n}\nclass Sneeze extends Annoyance {\n}\nclass Human {\n\tpublic static void main(String[] args)\n\tthrows Exception {\n\t\ttry {\n\t\t\ttry {\n\t\t\t\tthrow new Sneeze();\n\t\t\t} catch ( Annoyance a ) {\n\t\t\t\tSystem.out.println(\"Caught Annoyance\");\n\t\t\t\tthrow a;\n\t\t\t}\n\t\t} catch ( Sneeze s ) {\n\t\t\tSystem.out.println(\"Caught Sneeze\");\n\t\t\treturn ;\n\t\t} finally {\n\t\t\tSystem.out.println(\"Hello World!\");\n\t\t}\n\t}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ed3\u679c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Caught Annoyance\nCaught Sneeze\nHello World!<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">10. \u5e38\u89c1\u7684 RuntimeException \u6709\u54ea\u4e9b\uff1f<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>ClassCastException(\u7c7b\u8f6c\u6362\u5f02\u5e38)<\/li><li>IndexOutOfBoundsException(\u6570\u7ec4\u8d8a\u754c)<\/li><li>NullPointerException(\u7a7a\u6307\u9488)<\/li><li>ArrayStoreException(\u6570\u636e\u5b58\u50a8\u5f02\u5e38\uff0c\u64cd\u4f5c\u6570\u7ec4\u65f6\u7c7b\u578b\u4e0d\u4e00\u81f4)<\/li><li>\u8fd8\u6709IO\u64cd\u4f5c\u7684BufferOverflowException\u5f02\u5e38<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">11. Java\u5e38\u89c1\u5f02\u5e38\u6709\u54ea\u4e9b<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>java.lang.IllegalAccessError<\/code>\uff1a\u8fdd\u6cd5\u8bbf\u95ee\u9519\u8bef\u3002\u5f53\u4e00\u4e2a\u5e94\u7528\u8bd5\u56fe\u8bbf\u95ee\u3001\u4fee\u6539\u67d0\u4e2a\u7c7b\u7684\u57df\uff08Field\uff09\u6216\u8005\u8c03\u7528\u5176\u65b9\u6cd5\uff0c\u4f46\u662f\u53c8\u8fdd\u53cd\u57df\u6216\u65b9\u6cd5\u7684\u53ef\u89c1\u6027\u58f0\u660e\uff0c\u5219\u629b\u51fa\u8be5\u5f02\u5e38\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>java.lang.InstantiationError<\/code>\uff1a\u5b9e\u4f8b\u5316\u9519\u8bef\u3002\u5f53\u4e00\u4e2a\u5e94\u7528\u8bd5\u56fe\u901a\u8fc7Java\u7684new\u64cd\u4f5c\u7b26\u6784\u9020\u4e00\u4e2a\u62bd\u8c61\u7c7b\u6216\u8005\u63a5\u53e3\u65f6\u629b\u51fa\u8be5\u5f02\u5e38.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>java.lang.OutOfMemoryError<\/code>\uff1a\u5185\u5b58\u4e0d\u8db3\u9519\u8bef\u3002\u5f53\u53ef\u7528\u5185\u5b58\u4e0d\u8db3\u4ee5\u8ba9Java\u865a\u62df\u673a\u5206\u914d\u7ed9\u4e00\u4e2a\u5bf9\u8c61\u65f6\u629b\u51fa\u8be5\u9519\u8bef\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>java.lang.StackOverflowError<\/code>\uff1a\u5806\u6808\u6ea2\u51fa\u9519\u8bef\u3002\u5f53\u4e00\u4e2a\u5e94\u7528\u9012\u5f52\u8c03\u7528\u7684\u5c42\u6b21\u592a\u6df1\u800c\u5bfc\u81f4\u5806\u6808\u6ea2\u51fa\u6216\u8005\u9677\u5165\u6b7b\u5faa\u73af\u65f6\u629b\u51fa\u8be5\u9519\u8bef\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>java.lang.ClassCastException<\/code>\uff1a\u7c7b\u9020\u578b\u5f02\u5e38\u3002\u5047\u8bbe\u6709\u7c7bA\u548cB\uff08A\u4e0d\u662fB\u7684\u7236\u7c7b\u6216\u5b50\u7c7b\uff09\uff0cO\u662fA\u7684\u5b9e\u4f8b\uff0c\u90a3\u4e48\u5f53\u5f3a\u5236\u5c06O\u6784\u9020\u4e3a\u7c7bB\u7684\u5b9e\u4f8b\u65f6\u629b\u51fa\u8be5\u5f02\u5e38\u3002\u8be5\u5f02\u5e38\u7ecf\u5e38\u88ab\u79f0\u4e3a\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u5f02\u5e38\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>java.lang.ClassNotFoundException<\/code>\uff1a\u627e\u4e0d\u5230\u7c7b\u5f02\u5e38\u3002\u5f53\u5e94\u7528\u8bd5\u56fe\u6839\u636e\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u7c7b\u540d\u6784\u9020\u7c7b\uff0c\u800c\u5728\u904d\u5386CLASSPAH\u4e4b\u540e\u627e\u4e0d\u5230\u5bf9\u5e94\u540d\u79f0\u7684class\u6587\u4ef6\u65f6\uff0c\u629b\u51fa\u8be5\u5f02\u5e38\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>java.lang.ArithmeticException<\/code>\uff1a\u7b97\u672f\u6761\u4ef6\u5f02\u5e38\u3002\u8b6c\u5982\uff1a\u6574\u6570\u9664\u96f6\u7b49\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>java.lang.ArrayIndexOutOfBoundsException<\/code>\uff1a\u6570\u7ec4\u7d22\u5f15\u8d8a\u754c\u5f02\u5e38\u3002\u5f53\u5bf9\u6570\u7ec4\u7684\u7d22\u5f15\u503c\u4e3a\u8d1f\u6570\u6216\u5927\u4e8e\u7b49\u4e8e\u6570\u7ec4\u5927\u5c0f\u65f6\u629b\u51fa\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>java.lang.IndexOutOfBoundsException<\/code>\uff1a\u7d22\u5f15\u8d8a\u754c\u5f02\u5e38\u3002\u5f53\u8bbf\u95ee\u67d0\u4e2a\u5e8f\u5217\u7684\u7d22\u5f15\u503c\u5c0f\u4e8e0\u6216\u5927\u4e8e\u7b49\u4e8e\u5e8f\u5217\u5927\u5c0f\u65f6\uff0c\u629b\u51fa\u8be5\u5f02\u5e38\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>java.lang.InstantiationException<\/code>\uff1a\u5b9e\u4f8b\u5316\u5f02\u5e38\u3002\u5f53\u8bd5\u56fe\u901a\u8fc7newInstance()\u65b9\u6cd5\u521b\u5efa\u67d0\u4e2a\u7c7b\u7684\u5b9e\u4f8b\uff0c\u800c\u8be5\u7c7b\u662f\u4e00\u4e2a\u62bd\u8c61\u7c7b\u6216\u63a5\u53e3\u65f6\uff0c\u629b\u51fa\u8be5\u5f02\u5e38\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>java.lang.NoSuchFieldException<\/code>\uff1a\u5c5e\u6027\u4e0d\u5b58\u5728\u5f02\u5e38\u3002\u5f53\u8bbf\u95ee\u67d0\u4e2a\u7c7b\u7684\u4e0d\u5b58\u5728\u7684\u5c5e\u6027\u65f6\u629b\u51fa\u8be5\u5f02\u5e38\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>java.lang.NoSuchMethodException<\/code>\uff1a\u65b9\u6cd5\u4e0d\u5b58\u5728\u5f02\u5e38\u3002\u5f53\u8bbf\u95ee\u67d0\u4e2a\u7c7b\u7684\u4e0d\u5b58\u5728\u7684\u65b9\u6cd5\u65f6\u629b\u51fa\u8be5\u5f02\u5e38\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>java.lang.NullPointerException<\/code>\uff1a\u7a7a\u6307\u9488\u5f02\u5e38\u3002\u5f53\u5e94\u7528\u8bd5\u56fe\u5728\u8981\u6c42\u4f7f\u7528\u5bf9\u8c61\u7684\u5730\u65b9\u4f7f\u7528\u4e86null\u65f6\uff0c\u629b\u51fa\u8be5\u5f02\u5e38\u3002\u8b6c\u5982\uff1a\u8c03\u7528null\u5bf9\u8c61\u7684\u5b9e\u4f8b\u65b9\u6cd5\u3001\u8bbf\u95eenull\u5bf9\u8c61\u7684\u5c5e\u6027\u3001\u8ba1\u7b97null\u5bf9\u8c61\u7684\u957f\u5ea6\u3001\u4f7f\u7528throw\u8bed\u53e5\u629b\u51fanull\u7b49\u7b49\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>java.lang.NumberFormatException<\/code>\uff1a\u6570\u5b57\u683c\u5f0f\u5f02\u5e38\u3002\u5f53\u8bd5\u56fe\u5c06\u4e00\u4e2aString\u8f6c\u6362\u4e3a\u6307\u5b9a\u7684\u6570\u5b57\u7c7b\u578b\uff0c\u800c\u8be5\u5b57\u7b26\u4e32\u786e\u4e0d\u6ee1\u8db3\u6570\u5b57\u7c7b\u578b\u8981\u6c42\u7684\u683c\u5f0f\u65f6\uff0c\u629b\u51fa\u8be5\u5f02\u5e38\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>java.lang.StringIndexOutOfBoundsException<\/code>\uff1a\u5b57\u7b26\u4e32\u7d22\u5f15\u8d8a\u754c\u5f02\u5e38\u3002\u5f53\u4f7f\u7528\u7d22\u5f15\u503c\u8bbf\u95ee\u67d0\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\uff0c\u800c\u8be5\u7d22\u5f15\u503c\u5c0f\u4e8e0\u6216\u5927\u4e8e\u7b49\u4e8e\u5e8f\u5217\u5927\u5c0f\u65f6\uff0c\u629b\u51fa\u8be5\u5f02\u5e38\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Java\u5f02\u5e38\u5904\u7406\u6700\u4f73\u5b9e\u8df5<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Java \u4e2d\u5904\u7406\u5f02\u5e38\u5e76\u4e0d\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4e8b\u60c5\u3002\u4e0d\u4ec5\u4ec5\u521d\u5b66\u8005\u5f88\u96be\u7406\u89e3\uff0c\u5373\u4f7f\u4e00\u4e9b\u6709\u7ecf\u9a8c\u7684\u5f00\u53d1\u8005\u4e5f\u9700\u8981\u82b1\u8d39\u5f88\u591a\u65f6\u95f4\u6765\u601d\u8003\u5982\u4f55\u5904\u7406\u5f02\u5e38\uff0c\u5305\u62ec\u9700\u8981\u5904\u7406\u54ea\u4e9b\u5f02\u5e38\uff0c\u600e\u6837\u5904\u7406\u7b49\u7b49\u3002\u8fd9\u4e5f\u662f\u7edd\u5927\u591a\u6570\u5f00\u53d1\u56e2\u961f\u90fd\u4f1a\u5236\u5b9a\u4e00\u4e9b\u89c4\u5219\u6765\u89c4\u8303\u8fdb\u884c\u5f02\u5e38\u5904\u7406\u7684\u539f\u56e0\u3002\u800c\u56e2\u961f\u4e4b\u95f4\u7684\u8fd9\u4e9b\u89c4\u8303\u5f80\u5f80\u662f\u622a\u7136\u4e0d\u540c\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7ed9\u51fa\u51e0\u4e2a\u88ab\u5f88\u591a\u56e2\u961f\u4f7f\u7528\u7684\u5f02\u5e38\u5904\u7406\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u5728 finally \u5757\u4e2d\u6e05\u7406\u8d44\u6e90\u6216\u8005\u4f7f\u7528 try-with-resource \u8bed\u53e5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u4f7f\u7528\u7c7b\u4f3cInputStream\u8fd9\u79cd\u9700\u8981\u4f7f\u7528\u540e\u5173\u95ed\u7684\u8d44\u6e90\u65f6\uff0c\u4e00\u4e2a\u5e38\u89c1\u7684\u9519\u8bef\u5c31\u662f\u5728try\u5757\u7684\u6700\u540e\u5173\u95ed\u8d44\u6e90\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void doNotCloseResourceInTry() {\n    FileInputStream inputStream = null;\n    try {\n        File file = new File(\".\/tmp.txt\");\n        inputStream = new FileInputStream(file);\n        \/\/ use the inputStream to read a file\n        \/\/ do NOT do this\n        inputStream.close();\n    } catch (FileNotFoundException e) {\n        log.error(e);\n    } catch (IOException e) {\n        log.error(e);\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u95ee\u9898\u5c31\u662f\uff0c\u53ea\u6709\u6ca1\u6709\u5f02\u5e38\u629b\u51fa\u7684\u65f6\u5019\uff0c\u8fd9\u6bb5\u4ee3\u7801\u624d\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u3002try \u4ee3\u7801\u5757\u5185\u4ee3\u7801\u4f1a\u6b63\u5e38\u6267\u884c\uff0c\u5e76\u4e14\u8d44\u6e90\u53ef\u4ee5\u6b63\u5e38\u5173\u95ed\u3002\u4f46\u662f\uff0c\u4f7f\u7528 try \u4ee3\u7801\u5757\u662f\u6709\u539f\u56e0\u7684\uff0c\u4e00\u822c\u8c03\u7528\u4e00\u4e2a\u6216\u591a\u4e2a\u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u65b9\u6cd5\uff0c\u800c\u4e14\uff0c\u4f60\u81ea\u5df1\u4e5f\u53ef\u80fd\u4f1a\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\uff0c\u8fd9\u610f\u5473\u7740\u4ee3\u7801\u53ef\u80fd\u4e0d\u4f1a\u6267\u884c\u5230 try \u4ee3\u7801\u5757\u7684\u6700\u540e\u90e8\u5206\u3002\u7ed3\u679c\u5c31\u662f\uff0c\u4f60\u5e76\u6ca1\u6709\u5173\u95ed\u8d44\u6e90\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\uff0c\u4f60\u5e94\u8be5\u628a\u6e05\u7406\u5de5\u4f5c\u7684\u4ee3\u7801\u653e\u5230 finally \u91cc\u53bb\uff0c\u6216\u8005\u4f7f\u7528 try-with-resource \u7279\u6027\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1.1 \u4f7f\u7528 finally \u4ee3\u7801\u5757<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0e\u524d\u9762\u51e0\u884c try \u4ee3\u7801\u5757\u4e0d\u540c\uff0cfinally \u4ee3\u7801\u5757\u603b\u662f\u4f1a\u88ab\u6267\u884c\u3002\u4e0d\u7ba1 try \u4ee3\u7801\u5757\u6210\u529f\u6267\u884c\u4e4b\u540e\u8fd8\u662f\u4f60\u5728 catch \u4ee3\u7801\u5757\u4e2d\u5904\u7406\u5b8c\u5f02\u5e38\u540e\u90fd\u4f1a\u6267\u884c\u3002\u56e0\u6b64\uff0c\u4f60\u53ef\u4ee5\u786e\u4fdd\u4f60\u6e05\u7406\u4e86\u6240\u6709\u6253\u5f00\u7684\u8d44\u6e90\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void closeResourceInFinally() {\n    FileInputStream inputStream = null;\n    try {\n        File file = new File(\".\/tmp.txt\");\n        inputStream = new FileInputStream(file);\n        \/\/ use the inputStream to read a file\n    } catch (FileNotFoundException e) {\n        log.error(e);\n    } finally {\n        if (inputStream != null) {\n            try {\n                inputStream.close();\n            } catch (IOException e) {\n                log.error(e);\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">1.2 Java 7 \u7684 try-with-resource \u8bed\u6cd5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u7684\u8d44\u6e90\u5b9e\u73b0\u4e86 AutoCloseable \u63a5\u53e3\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u8bed\u6cd5\u3002\u5927\u591a\u6570\u7684 Java \u6807\u51c6\u8d44\u6e90\u90fd\u7ee7\u627f\u4e86\u8fd9\u4e2a\u63a5\u53e3\u3002\u5f53\u4f60\u5728 try \u5b50\u53e5\u4e2d\u6253\u5f00\u8d44\u6e90\uff0c\u8d44\u6e90\u4f1a\u5728 try \u4ee3\u7801\u5757\u6267\u884c\u540e\u6216\u5f02\u5e38\u5904\u7406\u540e\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void automaticallyCloseResource() {\n    File file = new File(\".\/tmp.txt\");\n    try (FileInputStream inputStream = new FileInputStream(file);) {\n        \/\/ use the inputStream to read a file\n    } catch (FileNotFoundException e) {\n        log.error(e);\n    } catch (IOException e) {\n        log.error(e);\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u4f18\u5148\u660e\u786e\u7684\u5f02\u5e38<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u629b\u51fa\u7684\u5f02\u5e38\u8d8a\u660e\u786e\u8d8a\u597d\uff0c\u6c38\u8fdc\u8bb0\u4f4f\uff0c\u4f60\u7684\u540c\u4e8b\u6216\u8005\u51e0\u4e2a\u6708\u4e4b\u540e\u7684\u4f60\uff0c\u5c06\u4f1a\u8c03\u7528\u4f60\u7684\u65b9\u6cd5\u5e76\u4e14\u5904\u7406\u5f02\u5e38\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u56e0\u6b64\u9700\u8981\u4fdd\u8bc1\u63d0\u4f9b\u7ed9\u4ed6\u4eec\u5c3d\u53ef\u80fd\u591a\u7684\u4fe1\u606f\u3002\u8fd9\u6837\u4f60\u7684 API \u66f4\u5bb9\u6613\u88ab\u7406\u89e3\u3002\u4f60\u7684\u65b9\u6cd5\u7684\u8c03\u7528\u8005\u80fd\u591f\u66f4\u597d\u7684\u5904\u7406\u5f02\u5e38\u5e76\u4e14\u907f\u514d\u989d\u5916\u7684\u68c0\u67e5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u56e0\u6b64\uff0c\u603b\u662f\u5c1d\u8bd5\u5bfb\u627e\u6700\u9002\u5408\u4f60\u7684\u5f02\u5e38\u4e8b\u4ef6\u7684\u7c7b\uff0c\u4f8b\u5982\uff0c\u629b\u51fa\u4e00\u4e2a NumberFormatException \u6765\u66ff\u6362\u4e00\u4e2a IllegalArgumentException \u3002\u907f\u514d\u629b\u51fa\u4e00\u4e2a\u4e0d\u660e\u786e\u7684\u5f02\u5e38\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void doNotDoThis() throws Exception {\n    ...\n}\npublic void doThis() throws NumberFormatException {\n    ...\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u5bf9\u5f02\u5e38\u8fdb\u884c\u6587\u6863\u8bf4\u660e<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u5728\u65b9\u6cd5\u4e0a\u58f0\u660e\u629b\u51fa\u5f02\u5e38\u65f6\uff0c\u4e5f\u9700\u8981\u8fdb\u884c\u6587\u6863\u8bf4\u660e\u3002\u76ee\u7684\u662f\u4e3a\u4e86\u7ed9\u8c03\u7528\u8005\u63d0\u4f9b\u5c3d\u53ef\u80fd\u591a\u7684\u4fe1\u606f\uff0c\u4ece\u800c\u53ef\u4ee5\u66f4\u597d\u5730\u907f\u514d\u6216\u5904\u7406\u5f02\u5e38\u3002<br>\n\u5728 Javadoc \u6dfb\u52a0 @throws \u58f0\u660e\uff0c\u5e76\u4e14\u63cf\u8ff0\u629b\u51fa\u5f02\u5e38\u7684\u573a\u666f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void doSomething(String input) throws MyBusinessException {\n    ...\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u4f7f\u7528\u63cf\u8ff0\u6027\u6d88\u606f\u629b\u51fa\u5f02\u5e38<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u629b\u51fa\u5f02\u5e38\u65f6\uff0c\u9700\u8981\u5c3d\u53ef\u80fd\u7cbe\u786e\u5730\u63cf\u8ff0\u95ee\u9898\u548c\u76f8\u5173\u4fe1\u606f\uff0c\u8fd9\u6837\u65e0\u8bba\u662f\u6253\u5370\u5230\u65e5\u5fd7\u4e2d\u8fd8\u662f\u5728\u76d1\u63a7\u5de5\u5177\u4e2d\uff0c\u90fd\u80fd\u591f\u66f4\u5bb9\u6613\u88ab\u4eba\u9605\u8bfb\uff0c\u4ece\u800c\u53ef\u4ee5\u66f4\u597d\u5730\u5b9a\u4f4d\u5177\u4f53\u9519\u8bef\u4fe1\u606f\u3001\u9519\u8bef\u7684\u4e25\u91cd\u7a0b\u5ea6\u7b49\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u8fd9\u91cc\u5e76\u4e0d\u662f\u8bf4\u8981\u5bf9\u9519\u8bef\u4fe1\u606f\u957f\u7bc7\u5927\u8bba\uff0c\u56e0\u4e3a\u672c\u6765 Exception \u7684\u7c7b\u540d\u5c31\u80fd\u591f\u53cd\u6620\u9519\u8bef\u7684\u539f\u56e0\uff0c\u56e0\u6b64\u53ea\u9700\u8981\u7528\u4e00\u5230\u4e24\u53e5\u8bdd\u63cf\u8ff0\u5373\u53ef\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u629b\u51fa\u4e00\u4e2a\u7279\u5b9a\u7684\u5f02\u5e38\uff0c\u5b83\u7684\u7c7b\u540d\u5f88\u53ef\u80fd\u5df2\u7ecf\u63cf\u8ff0\u4e86\u8fd9\u79cd\u9519\u8bef\u3002\u6240\u4ee5\uff0c\u4f60\u4e0d\u9700\u8981\u63d0\u4f9b\u5f88\u591a\u989d\u5916\u7684\u4fe1\u606f\u3002\u4e00\u4e2a\u5f88\u597d\u7684\u4f8b\u5b50\u662f NumberFormatException \u3002\u5f53\u4f60\u4ee5\u9519\u8bef\u7684\u683c\u5f0f\u63d0\u4f9b String \u65f6\uff0c\u5b83\u5c06\u88ab java.lang.Long \u7c7b\u7684\u6784\u9020\u51fd\u6570\u629b\u51fa\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n    new Long(\"xyz\");\n} catch (NumberFormatException e) {\n    log.error(e);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u4f18\u5148\u6355\u83b7\u6700\u5177\u4f53\u7684\u5f02\u5e38<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5927\u591a\u6570 IDE \u90fd\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5b9e\u73b0\u8fd9\u4e2a\u6700\u4f73\u5b9e\u8df5\u3002\u5f53\u4f60\u5c1d\u8bd5\u9996\u5148\u6355\u83b7\u8f83\u4e0d\u5177\u4f53\u7684\u5f02\u5e38\u65f6\uff0c\u5b83\u4eec\u4f1a\u62a5\u544a\u65e0\u6cd5\u8bbf\u95ee\u7684\u4ee3\u7801\u5757\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u95ee\u9898\u5728\u4e8e\uff0c\u53ea\u6709\u5339\u914d\u5f02\u5e38\u7684\u7b2c\u4e00\u4e2a catch \u5757\u4f1a\u88ab\u6267\u884c\u3002 \u56e0\u6b64\uff0c\u5982\u679c\u9996\u5148\u6355\u83b7 IllegalArgumentException \uff0c\u5219\u6c38\u8fdc\u4e0d\u4f1a\u5230\u8fbe\u5e94\u8be5\u5904\u7406\u66f4\u5177\u4f53\u7684 NumberFormatException \u7684 catch \u5757\uff0c\u56e0\u4e3a\u5b83\u662f IllegalArgumentException \u7684\u5b50\u7c7b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u603b\u662f\u4f18\u5148\u6355\u83b7\u6700\u5177\u4f53\u7684\u5f02\u5e38\u7c7b\uff0c\u5e76\u5c06\u4e0d\u592a\u5177\u4f53\u7684 catch \u5757\u6dfb\u52a0\u5230\u5217\u8868\u7684\u672b\u5c3e\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u53ef\u4ee5\u5728\u4e0b\u9762\u7684\u4ee3\u7801\u7247\u65ad\u4e2d\u770b\u5230\u8fd9\u6837\u4e00\u4e2a try-catch \u8bed\u53e5\u7684\u4f8b\u5b50\u3002 \u7b2c\u4e00\u4e2a catch \u5757\u5904\u7406\u6240\u6709 NumberFormatException \u5f02\u5e38\uff0c\u7b2c\u4e8c\u4e2a\u5904\u7406\u6240\u6709\u975e NumberFormatException \u5f02\u5e38\u7684IllegalArgumentException \u5f02\u5e38\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void catchMostSpecificExceptionFirst() {\n    try {\n        doSomething(\"A message\");\n    } catch (NumberFormatException e) {\n        log.error(e);\n    } catch (IllegalArgumentException e) {\n        log.error(e)\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6. \u4e0d\u8981\u6355\u83b7 Throwable \u7c7b<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Throwable \u662f\u6240\u6709\u5f02\u5e38\u548c\u9519\u8bef\u7684\u8d85\u7c7b\u3002\u4f60\u53ef\u4ee5\u5728 catch \u5b50\u53e5\u4e2d\u4f7f\u7528\u5b83\uff0c\u4f46\u662f\u4f60\u6c38\u8fdc\u4e0d\u5e94\u8be5\u8fd9\u6837\u505a\uff01<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u5728 catch \u5b50\u53e5\u4e2d\u4f7f\u7528 Throwable \uff0c\u5b83\u4e0d\u4ec5\u4f1a\u6355\u83b7\u6240\u6709\u5f02\u5e38\uff0c\u4e5f\u5c06\u6355\u83b7\u6240\u6709\u7684\u9519\u8bef\u3002JVM \u629b\u51fa\u9519\u8bef\uff0c\u6307\u51fa\u4e0d\u5e94\u8be5\u7531\u5e94\u7528\u7a0b\u5e8f\u5904\u7406\u7684\u4e25\u91cd\u95ee\u9898\u3002 \u5178\u578b\u7684\u4f8b\u5b50\u662f OutOfMemoryError \u6216\u8005 StackOverflowError \u3002\u4e24\u8005\u90fd\u662f\u7531\u5e94\u7528\u7a0b\u5e8f\u63a7\u5236\u4e4b\u5916\u7684\u60c5\u51b5\u5f15\u8d77\u7684\uff0c\u65e0\u6cd5\u5904\u7406\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\uff0c\u6700\u597d\u4e0d\u8981\u6355\u83b7 Throwable \uff0c\u9664\u975e\u4f60\u786e\u5b9a\u81ea\u5df1\u5904\u4e8e\u4e00\u79cd\u7279\u6b8a\u7684\u60c5\u51b5\u4e0b\u80fd\u591f\u5904\u7406\u9519\u8bef\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void doNotCatchThrowable() {\n    try {\n        \/\/ do something\n    } catch (Throwable t) {\n        \/\/ don't do this!\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">7. \u4e0d\u8981\u5ffd\u7565\u5f02\u5e38<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f88\u591a\u65f6\u5019\uff0c\u5f00\u53d1\u8005\u5f88\u6709\u81ea\u4fe1\u4e0d\u4f1a\u629b\u51fa\u5f02\u5e38\uff0c\u56e0\u6b64\u5199\u4e86\u4e00\u4e2acatch\u5757\uff0c\u4f46\u662f\u6ca1\u6709\u505a\u4efb\u4f55\u5904\u7406\u6216\u8005\u8bb0\u5f55\u65e5\u5fd7\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void doNotIgnoreExceptions() {\n    try {\n        \/\/ do something\n    } catch (NumberFormatException e) {\n        \/\/ this will never happen\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u73b0\u5b9e\u662f\u7ecf\u5e38\u4f1a\u51fa\u73b0\u65e0\u6cd5\u9884\u6599\u7684\u5f02\u5e38\uff0c\u6216\u8005\u65e0\u6cd5\u786e\u5b9a\u8fd9\u91cc\u7684\u4ee3\u7801\u672a\u6765\u662f\u4e0d\u662f\u4f1a\u6539\u52a8(\u5220\u9664\u4e86\u963b\u6b62\u5f02\u5e38\u629b\u51fa\u7684\u4ee3\u7801)\uff0c\u800c\u6b64\u65f6\u7531\u4e8e\u5f02\u5e38\u88ab\u6355\u83b7\uff0c\u4f7f\u5f97\u65e0\u6cd5\u62ff\u5230\u8db3\u591f\u7684\u9519\u8bef\u4fe1\u606f\u6765\u5b9a\u4f4d\u95ee\u9898\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5408\u7406\u7684\u505a\u6cd5\u662f\u81f3\u5c11\u8981\u8bb0\u5f55\u5f02\u5e38\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void logAnException() {\n    try {\n        \/\/ do something\n    } catch (NumberFormatException e) {\n        log.error(\"This should never happen: \" + e);\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">8. \u4e0d\u8981\u8bb0\u5f55\u5e76\u629b\u51fa\u5f02\u5e38<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u53ef\u80fd\u662f\u672c\u6587\u4e2d\u6700\u5e38\u88ab\u5ffd\u7565\u7684\u6700\u4f73\u5b9e\u8df5\u3002\u53ef\u4ee5\u53d1\u73b0\u5f88\u591a\u4ee3\u7801\u751a\u81f3\u7c7b\u5e93\u4e2d\u90fd\u4f1a\u6709\u6355\u83b7\u5f02\u5e38\u3001\u8bb0\u5f55\u65e5\u5fd7\u5e76\u518d\u6b21\u629b\u51fa\u7684\u903b\u8f91\u3002\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n    new Long(\"xyz\");\n} catch (NumberFormatException e) {\n    log.error(e);\n    throw e;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u5904\u7406\u903b\u8f91\u770b\u7740\u662f\u5408\u7406\u7684\u3002\u4f46\u8fd9\u7ecf\u5e38\u4f1a\u7ed9\u540c\u4e00\u4e2a\u5f02\u5e38\u8f93\u51fa\u591a\u6761\u65e5\u5fd7\u3002\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>17:44:28,945 ERROR TestExceptionHandling:65 - java.lang.NumberFormatException: For input string: \"xyz\"\nException in thread \"main\" java.lang.NumberFormatException: For input string: \"xyz\"\nat java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)\nat java.lang.Long.parseLong(Long.java:589)\nat java.lang.Long.(Long.java:965)\nat com.stackify.example.TestExceptionHandling.logAndThrowException(TestExceptionHandling.java:63)\nat com.stackify.example.TestExceptionHandling.main(TestExceptionHandling.java:58)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u4e0a\u6240\u793a\uff0c\u540e\u9762\u7684\u65e5\u5fd7\u4e5f\u6ca1\u6709\u9644\u52a0\u66f4\u6709\u7528\u7684\u4fe1\u606f\u3002\u5982\u679c\u60f3\u8981\u63d0\u4f9b\u66f4\u52a0\u6709\u7528\u7684\u4fe1\u606f\uff0c\u90a3\u4e48\u53ef\u4ee5\u5c06\u5f02\u5e38\u5305\u88c5\u4e3a\u81ea\u5b9a\u4e49\u5f02\u5e38\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void wrapException(String input) throws MyBusinessException {\n    try {\n        \/\/ do something\n    } catch (NumberFormatException e) {\n        throw new MyBusinessException(\"A message that describes the error.\", e);\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u56e0\u6b64\uff0c\u4ec5\u4ec5\u5f53\u60f3\u8981\u5904\u7406\u5f02\u5e38\u65f6\u624d\u53bb\u6355\u83b7\uff0c\u5426\u5219\u53ea\u9700\u8981\u5728\u65b9\u6cd5\u7b7e\u540d\u4e2d\u58f0\u660e\u8ba9\u8c03\u7528\u8005\u53bb\u5904\u7406\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">9. \u5305\u88c5\u5f02\u5e38\u65f6\u4e0d\u8981\u629b\u5f03\u539f\u59cb\u7684\u5f02\u5e38<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6355\u83b7\u6807\u51c6\u5f02\u5e38\u5e76\u5305\u88c5\u4e3a\u81ea\u5b9a\u4e49\u5f02\u5e38\u662f\u4e00\u4e2a\u5f88\u5e38\u89c1\u7684\u505a\u6cd5\u3002\u8fd9\u6837\u53ef\u4ee5\u6dfb\u52a0\u66f4\u4e3a\u5177\u4f53\u7684\u5f02\u5e38\u4fe1\u606f\u5e76\u80fd\u591f\u505a\u9488\u5bf9\u7684\u5f02\u5e38\u5904\u7406\u3002<br>\n\u5728\u4f60\u8fd9\u6837\u505a\u65f6\uff0c\u8bf7\u786e\u4fdd\u5c06\u539f\u59cb\u5f02\u5e38\u8bbe\u7f6e\u4e3a\u539f\u56e0\uff08\u6ce8\uff1a\u53c2\u8003\u4e0b\u65b9\u4ee3\u7801 NumberFormatException e \u4e2d\u7684\u539f\u59cb\u5f02\u5e38 e \uff09\u3002Exception \u7c7b\u63d0\u4f9b\u4e86\u7279\u6b8a\u7684\u6784\u9020\u51fd\u6570\u65b9\u6cd5\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a Throwable \u4f5c\u4e3a\u53c2\u6570\u3002\u5426\u5219\uff0c\u4f60\u5c06\u4f1a\u4e22\u5931\u5806\u6808\u8ddf\u8e2a\u548c\u539f\u59cb\u5f02\u5e38\u7684\u6d88\u606f\uff0c\u8fd9\u5c06\u4f1a\u4f7f\u5206\u6790\u5bfc\u81f4\u5f02\u5e38\u7684\u5f02\u5e38\u4e8b\u4ef6\u53d8\u5f97\u56f0\u96be\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void wrapException(String input) throws MyBusinessException {\n    try {\n        \/\/ do something\n    } catch (NumberFormatException e) {\n        throw new MyBusinessException(\"A message that describes the error.\", e);\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">10. \u4e0d\u8981\u4f7f\u7528\u5f02\u5e38\u63a7\u5236\u7a0b\u5e8f\u7684\u6d41\u7a0b<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u5e94\u8be5\u4f7f\u7528\u5f02\u5e38\u63a7\u5236\u5e94\u7528\u7684\u6267\u884c\u6d41\u7a0b\uff0c\u4f8b\u5982\uff0c\u672c\u5e94\u8be5\u4f7f\u7528if\u8bed\u53e5\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\u7684\u60c5\u51b5\u4e0b\uff0c\u4f60\u5374\u4f7f\u7528\u5f02\u5e38\u5904\u7406\uff0c\u8fd9\u662f\u975e\u5e38\u4e0d\u597d\u7684\u4e60\u60ef\uff0c\u4f1a\u4e25\u91cd\u5f71\u54cd\u5e94\u7528\u7684\u6027\u80fd\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">11. \u4f7f\u7528\u6807\u51c6\u5f02\u5e38<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f7f\u7528\u5185\u5efa\u7684\u5f02\u5e38\u53ef\u4ee5\u89e3\u51b3\u95ee\u9898\uff0c\u5c31\u4e0d\u8981\u5b9a\u4e49\u81ea\u5df1\u7684\u5f02\u5e38\u3002Java API \u63d0\u4f9b\u4e86\u4e0a\u767e\u79cd\u9488\u5bf9\u4e0d\u540c\u60c5\u51b5\u7684\u5f02\u5e38\u7c7b\u578b\uff0c\u5728\u5f00\u53d1\u4e2d\u9996\u5148\u5c3d\u53ef\u80fd\u4f7f\u7528 Java API \u63d0\u4f9b\u7684\u5f02\u5e38\uff0c\u5982\u679c\u6807\u51c6\u7684\u5f02\u5e38\u4e0d\u80fd\u6ee1\u8db3\u4f60\u7684\u8981\u6c42\uff0c\u8fd9\u65f6\u5019\u521b\u5efa\u81ea\u5df1\u7684\u5b9a\u5236\u5f02\u5e38\u3002\u5c3d\u53ef\u80fd\u5f97\u4f7f\u7528\u6807\u51c6\u5f02\u5e38\u6709\u5229\u4e8e\u65b0\u52a0\u5165\u7684\u5f00\u53d1\u8005\u770b\u61c2\u9879\u76ee\u4ee3\u7801\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">12. \u5f02\u5e38\u4f1a\u5f71\u54cd\u6027\u80fd<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f02\u5e38\u5904\u7406\u7684\u6027\u80fd\u6210\u672c\u975e\u5e38\u9ad8\uff0c\u6bcf\u4e2a Java \u7a0b\u5e8f\u5458\u5728\u5f00\u53d1\u65f6\u90fd\u5e94\u7262\u8bb0\u8fd9\u53e5\u8bdd\u3002\u521b\u5efa\u4e00\u4e2a\u5f02\u5e38\u975e\u5e38\u6162\uff0c\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u53c8\u4f1a\u6d88\u80171~5ms\uff0c\u5f53\u4e00\u4e2a\u5f02\u5e38\u5728\u5e94\u7528\u7684\u591a\u4e2a\u5c42\u7ea7\u4e4b\u95f4\u4f20\u9012\u65f6\uff0c\u4f1a\u62d6\u7d2f\u6574\u4e2a\u5e94\u7528\u7684\u6027\u80fd\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u4ec5\u5728\u5f02\u5e38\u60c5\u51b5\u4e0b\u4f7f\u7528\u5f02\u5e38\uff1b<\/li><li>\u5728\u53ef\u6062\u590d\u7684\u5f02\u5e38\u60c5\u51b5\u4e0b\u4f7f\u7528\u5f02\u5e38\uff1b<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c3d\u7ba1\u4f7f\u7528\u5f02\u5e38\u6709\u5229\u4e8e Java \u5f00\u53d1\uff0c\u4f46\u662f\u5728\u5e94\u7528\u4e2d\u6700\u597d\u4e0d\u8981\u6355\u83b7\u592a\u591a\u7684\u8c03\u7528\u6808\uff0c\u56e0\u4e3a\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\u90fd\u4e0d\u9700\u8981\u6253\u5370\u8c03\u7528\u6808\u5c31\u77e5\u9053\u54ea\u91cc\u51fa\u9519\u4e86\u3002\u56e0\u6b64\uff0c\u5f02\u5e38\u6d88\u606f\u5e94\u8be5\u63d0\u4f9b\u6070\u5230\u597d\u5904\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">13. \u603b\u7ed3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u7efc\u4e0a\u6240\u8ff0\uff0c\u5f53\u4f60\u629b\u51fa\u6216\u6355\u83b7\u5f02\u5e38\u7684\u65f6\u5019\uff0c\u6709\u5f88\u591a\u4e0d\u540c\u7684\u60c5\u51b5\u9700\u8981\u8003\u8651\uff0c\u800c\u4e14\u5927\u90e8\u5206\u4e8b\u60c5\u90fd\u662f\u4e3a\u4e86\u6539\u5584\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u6216\u8005 API \u7684\u53ef\u7528\u6027\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f02\u5e38\u4e0d\u4ec5\u4ec5\u662f\u4e00\u4e2a\u9519\u8bef\u63a7\u5236\u673a\u5236\uff0c\u4e5f\u662f\u4e00\u4e2a\u901a\u4fe1\u5a92\u4ecb\u3002\u56e0\u6b64\uff0c\u4e3a\u4e86\u548c\u540c\u4e8b\u66f4\u597d\u7684\u5408\u4f5c\uff0c\u4e00\u4e2a\u56e2\u961f\u5fc5\u987b\u8981\u5236\u5b9a\u51fa\u4e00\u4e2a\u6700\u4f73\u5b9e\u8df5\u548c\u89c4\u5219\uff0c\u53ea\u6709\u8fd9\u6837\uff0c\u56e2\u961f\u6210\u5458\u624d\u80fd\u7406\u89e3\u8fd9\u4e9b\u901a\u7528\u6982\u5ff5\uff0c\u540c\u65f6\u5728\u5de5\u4f5c\u4e2d\u4f7f\u7528\u5b83\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5f02\u5e38\u5904\u7406-\u963f\u91cc\u5df4\u5df4Java\u5f00\u53d1\u624b\u518c<\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li>\u3010\u5f3a\u5236\u3011Java \u7c7b\u5e93\u4e2d\u5b9a\u4e49\u7684\u53ef\u4ee5\u901a\u8fc7\u9884\u68c0\u67e5\u65b9\u5f0f\u89c4\u907f\u7684RuntimeException\u5f02\u5e38\u4e0d\u5e94\u8be5\u901a\u8fc7catch \u7684\u65b9\u5f0f\u6765\u5904\u7406\uff0c\u6bd4\u5982\uff1aNullPointerException\uff0cIndexOutOfBoundsException\u7b49\u7b49\u3002 \u8bf4\u660e\uff1a\u65e0\u6cd5\u901a\u8fc7\u9884\u68c0\u67e5\u7684\u5f02\u5e38\u9664\u5916\uff0c\u6bd4\u5982\uff0c\u5728\u89e3\u6790\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u6570\u5b57\u65f6\uff0c\u53ef\u80fd\u5b58\u5728\u6570\u5b57\u683c\u5f0f\u9519\u8bef\uff0c\u4e0d\u5f97\u4e0d\u901a\u8fc7catch NumberFormatException\u6765\u5b9e\u73b0\u3002 \u6b63\u4f8b\uff1aif (obj != null) {\u2026} \u53cd\u4f8b\uff1atry { obj.method(); } catch (NullPointerException e) {\u2026}\n<\/li><li>\u3010\u5f3a\u5236\u3011\u5f02\u5e38\u4e0d\u8981\u7528\u6765\u505a\u6d41\u7a0b\u63a7\u5236\uff0c\u6761\u4ef6\u63a7\u5236\u3002 \u8bf4\u660e\uff1a\u5f02\u5e38\u8bbe\u8ba1\u7684\u521d\u8877\u662f\u89e3\u51b3\u7a0b\u5e8f\u8fd0\u884c\u4e2d\u7684\u5404\u79cd\u610f\u5916\u60c5\u51b5\uff0c\u4e14\u5f02\u5e38\u7684\u5904\u7406\u6548\u7387\u6bd4\u6761\u4ef6\u5224\u65ad\u65b9\u5f0f\u8981\u4f4e\u5f88\u591a\u3002\n<\/li><li>\u3010\u5f3a\u5236\u3011catch\u65f6\u8bf7\u5206\u6e05\u7a33\u5b9a\u4ee3\u7801\u548c\u975e\u7a33\u5b9a\u4ee3\u7801\uff0c\u7a33\u5b9a\u4ee3\u7801\u6307\u7684\u662f\u65e0\u8bba\u5982\u4f55\u4e0d\u4f1a\u51fa\u9519\u7684\u4ee3\u7801\u3002\u5bf9\u4e8e\u975e\u7a33\u5b9a\u4ee3\u7801\u7684catch\u5c3d\u53ef\u80fd\u8fdb\u884c\u533a\u5206\u5f02\u5e38\u7c7b\u578b\uff0c\u518d\u505a\u5bf9\u5e94\u7684\u5f02\u5e38\u5904\u7406\u3002 \u8bf4\u660e\uff1a\u5bf9\u5927\u6bb5\u4ee3\u7801\u8fdb\u884ctry-catch\uff0c\u4f7f\u7a0b\u5e8f\u65e0\u6cd5\u6839\u636e\u4e0d\u540c\u7684\u5f02\u5e38\u505a\u51fa\u6b63\u786e\u7684\u5e94\u6fc0\u53cd\u5e94\uff0c\u4e5f\u4e0d\u5229\u4e8e\u5b9a\u4f4d\u95ee\u9898\uff0c\u8fd9\u662f\u4e00\u79cd\u4e0d\u8d1f\u8d23\u4efb\u7684\u8868\u73b0\u3002 \u6b63\u4f8b\uff1a\u7528\u6237\u6ce8\u518c\u7684\u573a\u666f\u4e2d\uff0c\u5982\u679c\u7528\u6237\u8f93\u5165\u975e\u6cd5\u5b57\u7b26\uff0c\u6216\u7528\u6237\u540d\u79f0\u5df2\u5b58\u5728\uff0c\u6216\u7528\u6237\u8f93\u5165\u5bc6\u7801\u8fc7\u4e8e\u7b80\u5355\uff0c\u5728\u7a0b\u5e8f\u4e0a\u4f5c\u51fa\u5206\u95e8\u522b\u7c7b\u7684\u5224\u65ad\uff0c\u5e76\u63d0\u793a\u7ed9\u7528\u6237\u3002\n<\/li><li>\u3010\u5f3a\u5236\u3011\u6355\u83b7\u5f02\u5e38\u662f\u4e3a\u4e86\u5904\u7406\u5b83\uff0c\u4e0d\u8981\u6355\u83b7\u4e86\u5374\u4ec0\u4e48\u90fd\u4e0d\u5904\u7406\u800c\u629b\u5f03\u4e4b\uff0c\u5982\u679c\u4e0d\u60f3\u5904\u7406\u5b83\uff0c\u8bf7\u5c06\u8be5\u5f02\u5e38\u629b\u7ed9\u5b83\u7684\u8c03\u7528\u8005\u3002\u6700\u5916\u5c42\u7684\u4e1a\u52a1\u4f7f\u7528\u8005\uff0c\u5fc5\u987b\u5904\u7406\u5f02\u5e38\uff0c\u5c06\u5176\u8f6c\u5316\u4e3a\u7528\u6237\u53ef\u4ee5\u7406\u89e3\u7684\u5185\u5bb9\u3002\n<\/li><li>\u3010\u5f3a\u5236\u3011\u6709try\u5757\u653e\u5230\u4e86\u4e8b\u52a1\u4ee3\u7801\u4e2d\uff0ccatch\u5f02\u5e38\u540e\uff0c\u5982\u679c\u9700\u8981\u56de\u6eda\u4e8b\u52a1\uff0c\u4e00\u5b9a\u8981\u6ce8\u610f\u624b\u52a8\u56de\u6eda\u4e8b\u52a1\u3002\n<\/li><li>\u3010\u5f3a\u5236\u3011finally\u5757\u5fc5\u987b\u5bf9\u8d44\u6e90\u5bf9\u8c61\u3001\u6d41\u5bf9\u8c61\u8fdb\u884c\u5173\u95ed\uff0c\u6709\u5f02\u5e38\u4e5f\u8981\u505atry-catch\u3002 \u8bf4\u660e\uff1a\u5982\u679cJDK7\u53ca\u4ee5\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528try-with-resources\u65b9\u5f0f\u3002\n<\/li><li>\u3010\u5f3a\u5236\u3011\u4e0d\u8981\u5728finally\u5757\u4e2d\u4f7f\u7528return\u3002 \u8bf4\u660e\uff1atry\u5757\u4e2d\u7684return\u8bed\u53e5\u6267\u884c\u6210\u529f\u540e\uff0c\u5e76\u4e0d\u9a6c\u4e0a\u8fd4\u56de\uff0c\u800c\u662f\u7ee7\u7eed\u6267\u884cfinally\u5757\u4e2d\u7684\u8bed\u53e5\uff0c\u5982\u679c\u6b64\u5904\u5b58\u5728return\u8bed\u53e5\uff0c\u5219\u5728\u6b64\u76f4\u63a5\u8fd4\u56de\uff0c\u65e0\u60c5\u4e22\u5f03\u6389try\u5757\u4e2d\u7684\u8fd4\u56de\u70b9\u3002 \u53cd\u4f8b\uff1a\n<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>private int x = 0;\npublic int checkReturn() {\n    try {\n        \/\/ x\u7b49\u4e8e1\uff0c\u6b64\u5904\u4e0d\u8fd4\u56de\n        return ++x;\n    } finally {\n        \/\/ \u8fd4\u56de\u7684\u7ed3\u679c\u662f2\n        return ++x;\n    }\n}<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\"><li>\u3010\u5f3a\u5236\u3011\u6355\u83b7\u5f02\u5e38\u4e0e\u629b\u5f02\u5e38\uff0c\u5fc5\u987b\u662f\u5b8c\u5168\u5339\u914d\uff0c\u6216\u8005\u6355\u83b7\u5f02\u5e38\u662f\u629b\u5f02\u5e38\u7684\u7236\u7c7b\u3002 \u8bf4\u660e\uff1a\u5982\u679c\u9884\u671f\u5bf9\u65b9\u629b\u7684\u662f\u7ee3\u7403\uff0c\u5b9e\u9645\u63a5\u5230\u7684\u662f\u94c5\u7403\uff0c\u5c31\u4f1a\u4ea7\u751f\u610f\u5916\u60c5\u51b5\u3002\n<\/li><li>\u3010\u5f3a\u5236\u3011\u5728\u8c03\u7528RPC\u3001\u4e8c\u65b9\u5305\u3001\u6216\u52a8\u6001\u751f\u6210\u7c7b\u7684\u76f8\u5173\u65b9\u6cd5\u65f6\uff0c\u6355\u6349\u5f02\u5e38\u5fc5\u987b\u4f7f\u7528Throwable\u7c7b\u6765\u8fdb\u884c\u62e6\u622a\u3002 \u8bf4\u660e\uff1a\u901a\u8fc7\u53cd\u5c04\u673a\u5236\u6765\u8c03\u7528\u65b9\u6cd5\uff0c\u5982\u679c\u627e\u4e0d\u5230\u65b9\u6cd5\uff0c\u629b\u51faNoSuchMethodException\u3002\u4ec0\u4e48\u60c5\u51b5\u4f1a\u629b\u51faNoSuchMethodError\u5462\uff1f\u4e8c\u65b9\u5305\u5728\u7c7b\u51b2\u7a81\u65f6\uff0c\u4ef2\u88c1\u673a\u5236\u53ef\u80fd\u5bfc\u81f4\u5f15\u5165\u975e\u9884\u671f\u7684\u7248\u672c\u4f7f\u7c7b\u7684\u65b9\u6cd5\u7b7e\u540d\u4e0d\u5339\u914d\uff0c\u6216\u8005\u5728\u5b57\u8282\u7801\u4fee\u6539\u6846\u67b6\uff08\u6bd4\u5982\uff1aASM\uff09\u52a8\u6001\u521b\u5efa\u6216\u4fee\u6539\u7c7b\u65f6\uff0c\u4fee\u6539\u4e86\u76f8\u5e94\u7684\u65b9\u6cd5\u7b7e\u540d\u3002\u8fd9\u4e9b\u60c5\u51b5\uff0c\u5373\u4f7f\u4ee3\u7801\u7f16\u8bd1\u671f\u662f\u6b63\u786e\u7684\uff0c\u4f46\u5728\u4ee3\u7801\u8fd0\u884c\u671f\u65f6\uff0c\u4f1a\u629b\u51faNoSuchMethodError\u3002\n<\/li><li>\u3010\u63a8\u8350\u3011\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u53ef\u4ee5\u4e3anull\uff0c\u4e0d\u5f3a\u5236\u8fd4\u56de\u7a7a\u96c6\u5408\uff0c\u6216\u8005\u7a7a\u5bf9\u8c61\u7b49\uff0c\u5fc5\u987b\u6dfb\u52a0\u6ce8\u91ca\u5145\u5206\u8bf4\u660e\u4ec0\u4e48\u60c5\u51b5\u4e0b\u4f1a\u8fd4\u56denull\u503c\u3002 \u8bf4\u660e\uff1a\u672c\u624b\u518c\u660e\u786e\u9632\u6b62NPE\u662f\u8c03\u7528\u8005\u7684\u8d23\u4efb\u3002\u5373\u4f7f\u88ab\u8c03\u7528\u65b9\u6cd5\u8fd4\u56de\u7a7a\u96c6\u5408\u6216\u8005\u7a7a\u5bf9\u8c61\uff0c\u5bf9\u8c03\u7528\u8005\u6765\u8bf4\uff0c\u4e5f\u5e76\u975e\u9ad8\u6795\u65e0\u5fe7\uff0c\u5fc5\u987b\u8003\u8651\u5230\u8fdc\u7a0b\u8c03\u7528\u5931\u8d25\u3001\u5e8f\u5217\u5316\u5931\u8d25\u3001\u8fd0\u884c\u65f6\u5f02\u5e38\u7b49\u573a\u666f\u8fd4\u56denull\u7684\u60c5\u51b5\u3002\n<\/li><li>\u3010\u63a8\u8350\u3011\u9632\u6b62NPE\uff0c\u662f\u7a0b\u5e8f\u5458\u7684\u57fa\u672c\u4fee\u517b\uff0c\u6ce8\u610fNPE\u4ea7\u751f\u7684\u573a\u666f\uff1a 1\uff09 \u8fd4\u56de\u7c7b\u578b\u4e3a\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0creturn\u5305\u88c5\u6570\u636e\u7c7b\u578b\u7684\u5bf9\u8c61\u65f6\uff0c\u81ea\u52a8\u62c6\u7bb1\u6709\u53ef\u80fd\u4ea7\u751fNPE\u3002 \u53cd\u4f8b\uff1apublic int f() { return Integer\u5bf9\u8c61}\uff0c \u5982\u679c\u4e3anull\uff0c\u81ea\u52a8\u89e3\u7bb1\u629bNPE\u3002 2\uff09 \u6570\u636e\u5e93\u7684\u67e5\u8be2\u7ed3\u679c\u53ef\u80fd\u4e3anull\u3002 3\uff09 \u96c6\u5408\u91cc\u7684\u5143\u7d20\u5373\u4f7fisNotEmpty\uff0c\u53d6\u51fa\u7684\u6570\u636e\u5143\u7d20\u4e5f\u53ef\u80fd\u4e3anull\u3002 4\uff09 \u8fdc\u7a0b\u8c03\u7528\u8fd4\u56de\u5bf9\u8c61\u65f6\uff0c\u4e00\u5f8b\u8981\u6c42\u8fdb\u884c\u7a7a\u6307\u9488\u5224\u65ad\uff0c\u9632\u6b62NPE\u3002 5\uff09 \u5bf9\u4e8eSession\u4e2d\u83b7\u53d6\u7684\u6570\u636e\uff0c\u5efa\u8bae\u8fdb\u884cNPE\u68c0\u67e5\uff0c\u907f\u514d\u7a7a\u6307\u9488\u3002 6\uff09 \u7ea7\u8054\u8c03\u7528obj.getA().getB().getC()\uff1b\u4e00\u8fde\u4e32\u8c03\u7528\uff0c\u6613\u4ea7\u751fNPE\u3002\n<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>\u6b63\u4f8b\uff1a\u4f7f\u7528JDK8\u7684Optional\u7c7b\u6765\u9632\u6b62NPE\u95ee\u9898\u3002<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\"><li>\u3010\u63a8\u8350\u3011\u5b9a\u4e49\u65f6\u533a\u5206unchecked \/ checked \u5f02\u5e38\uff0c\u907f\u514d\u76f4\u63a5\u629b\u51fanew RuntimeException()\uff0c\u66f4\u4e0d\u5141\u8bb8\u629b\u51faException\u6216\u8005Throwable\uff0c\u5e94\u4f7f\u7528\u6709\u4e1a\u52a1\u542b\u4e49\u7684\u81ea\u5b9a\u4e49\u5f02\u5e38\u3002\u63a8\u8350\u4e1a\u754c\u5df2\u5b9a\u4e49\u8fc7\u7684\u81ea\u5b9a\u4e49\u5f02\u5e38\uff0c\u5982\uff1aDAOException \/ ServiceException\u7b49\u3002\n<\/li><li>\u3010\u53c2\u8003\u3011\u5bf9\u4e8e\u516c\u53f8\u5916\u7684http\/api\u5f00\u653e\u63a5\u53e3\u5fc5\u987b\u4f7f\u7528\u201c\u9519\u8bef\u7801\u201d\uff1b\u800c\u5e94\u7528\u5185\u90e8\u63a8\u8350\u5f02\u5e38\u629b\u51fa\uff1b\u8de8\u5e94\u7528\u95f4RPC\u8c03\u7528\u4f18\u5148\u8003\u8651\u4f7f\u7528Result\u65b9\u5f0f\uff0c\u5c01\u88c5isSuccess()\u65b9\u6cd5\u3001\u201c\u9519\u8bef\u7801\u201d\u3001\u201c\u9519\u8bef\u7b80\u77ed\u4fe1\u606f\u201d\u3002 \u8bf4\u660e\uff1a\u5173\u4e8eRPC\u65b9\u6cd5\u8fd4\u56de\u65b9\u5f0f\u4f7f\u7528Result\u65b9\u5f0f\u7684\u7406\u7531\uff1a 1\uff09\u4f7f\u7528\u629b\u5f02\u5e38\u8fd4\u56de\u65b9\u5f0f\uff0c\u8c03\u7528\u65b9\u5982\u679c\u6ca1\u6709\u6355\u83b7\u5230\u5c31\u4f1a\u4ea7\u751f\u8fd0\u884c\u65f6\u9519\u8bef\u3002 2\uff09\u5982\u679c\u4e0d\u52a0\u6808\u4fe1\u606f\uff0c\u53ea\u662fnew\u81ea\u5b9a\u4e49\u5f02\u5e38\uff0c\u52a0\u5165\u81ea\u5df1\u7684\u7406\u89e3\u7684error message\uff0c\u5bf9\u4e8e\u8c03\u7528\u7aef\u89e3\u51b3\u95ee\u9898\u7684\u5e2e\u52a9\u4e0d\u4f1a\u592a\u591a\u3002\u5982\u679c\u52a0\u4e86\u6808\u4fe1\u606f\uff0c\u5728\u9891\u7e41\u8c03\u7528\u51fa\u9519\u7684\u60c5\u51b5\u4e0b\uff0c\u6570\u636e\u5e8f\u5217\u5316\u548c\u4f20\u8f93\u7684\u6027\u80fd\u635f\u8017\u4e5f\u662f\u95ee\u9898\u3002\n<\/li><li>\u3010\u53c2\u8003\u3011\u907f\u514d\u51fa\u73b0\u91cd\u590d\u7684\u4ee3\u7801\uff08Don\u2019t Repeat Yourself\uff09\uff0c\u5373DRY\u539f\u5219\u3002 \u8bf4\u660e\uff1a\u968f\u610f\u590d\u5236\u548c\u7c98\u8d34\u4ee3\u7801\uff0c\u5fc5\u7136\u4f1a\u5bfc\u81f4\u4ee3\u7801\u7684\u91cd\u590d\uff0c\u5728\u4ee5\u540e\u9700\u8981\u4fee\u6539\u65f6\uff0c\u9700\u8981\u4fee\u6539\u6240\u6709\u7684\u526f\u672c\uff0c\u5bb9\u6613\u9057\u6f0f\u3002\u5fc5\u8981\u65f6\u62bd\u53d6\u5171\u6027\u65b9\u6cd5\uff0c\u6216\u8005\u62bd\u8c61\u516c\u5171\u7c7b\uff0c\u751a\u81f3\u662f\u7ec4\u4ef6\u5316\u3002 \u6b63\u4f8b\uff1a\u4e00\u4e2a\u7c7b\u4e2d\u6709\u591a\u4e2apublic\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u8fdb\u884c\u6570\u884c\u76f8\u540c\u7684\u53c2\u6570\u6821\u9a8c\u64cd\u4f5c\uff0c\u8fd9\u4e2a\u65f6\u5019\u8bf7\u62bd\u53d6\uff1a\n<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><code>private boolean checkParam(DTO dto) {\u2026}<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9488\u5bf9\u672c\u6587\u5982\u6709\u5185\u5bb9\u9519\u8bef\u4e89\u8bae\uff0c\u6b22\u8fce\u52a0\u6211\u5fae\u4fe1\uff1axttblog2\uff0c\u8fdb\u884c\u6307\u6b63\u66f4\u65b0\u3002\u540e\u7eed\u66f4\u65b0\u7248\u672c\uff0c\u5c06\u540c\u6b65\u5728\u5fae\u4fe1\u516c\u4f17\u53f7\u4e2d\uff01<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=5302\">Java\u5f02\u5e38\u9762\u8bd5\u9898\uff082022\u6700\u65b0\u7248\uff09<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>Java\u5f02\u5e38\u67b6\u6784\u4e0e\u5f02\u5e38\u5173\u952e\u5b57 Java\u5f02\u5e38\u7b80\u4ecb Java\u5f02\u5e38\u662fJava\u63d0\u4f9b\u7684\u4e00\u79cd\u8bc6\u522b\u53ca\u54cd\u5e94\u9519\u8bef\u7684\u4e00\u81f4\u6027\u673a\u5236\u3002  [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[472,546,579,62,410],"class_list":["post-5302","post","type-post","status-publish","format-standard","hentry","category-java","tag-rest","tag-546","tag-579","tag-62","tag-410"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5302","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5302"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5302\/revisions"}],"predecessor-version":[{"id":5303,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5302\/revisions\/5303"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}