{"id":4100,"date":"2024-04-19T08:42:54","date_gmt":"2024-04-19T00:42:54","guid":{"rendered":"https:\/\/xinchewhd.com.cn\/index.php\/%e9%9d%a2%e8%af%95\/%e9%9d%a2%e8%af%95%e5%ae%98%ef%bc%9ajava-%e4%b8%ad-throw-%e4%b8%8e-throws-%e7%9a%84%e5%8c%ba%e5%88%ab%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%9f\/"},"modified":"2024-04-19T08:42:54","modified_gmt":"2024-04-19T00:42:54","slug":"%e9%9d%a2%e8%af%95%e5%ae%98%ef%bc%9ajava-%e4%b8%ad-throw-%e4%b8%8e-throws-%e7%9a%84%e5%8c%ba%e5%88%ab%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/xinchewhd.com.cn\/index.php\/%e9%9d%a2%e8%af%95\/%e9%9d%a2%e8%af%95%e5%ae%98%ef%bc%9ajava-%e4%b8%ad-throw-%e4%b8%8e-throws-%e7%9a%84%e5%8c%ba%e5%88%ab%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%9f\/","title":{"rendered":"\u9762\u8bd5\u5b98\uff1aJava \u4e2d throw \u4e0e throws \u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f"},"content":{"rendered":"<div class='wxsyncmain'>\n<p data-v-md-line=\"1\"><strong><span style=\"font-size: 20px;\">throw\u5173\u952e\u5b57<\/span><\/strong><\/p>\n<ul class=\"list-paddingleft-1\" style=\"list-style-type: disc;\">\n<li>\n<p style=\"line-height: normal;\"><strong><span style=\"font-size: 15px;\">\u7528\u9014<\/span><\/strong><span style=\"font-size: 15px;\">\uff1a\u5728\u4ee3\u7801\u5757\u5185\u90e8\u663e\u5f0f\u5730\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u5bf9\u8c61\u3002<\/span><\/p>\n<\/li>\n<li>\n<p style=\"line-height: normal;\"><strong><span style=\"font-size: 15px;letter-spacing: 0.034em;\">\u8bed\u6cd5<\/span><\/strong><span style=\"font-size: 15px;letter-spacing: 0.034em;\">\uff1athrow new ExceptionName(message);<\/span><\/p>\n<\/li>\n<\/ul>\n<p style=\"line-height: normal;\"><span style=\"font-size: 15px;letter-spacing: 0.034em;\"><\/span><\/p>\n<section style=\"margin-top: 24px;\"><strong><span style=\"font-size: 20px;\">throws\u5173\u952e\u5b57<\/span><\/strong><\/section>\n<ul class=\"list-paddingleft-1\" style=\"list-style-type: disc;\">\n<li>\n<p style=\"line-height: normal;\"><strong><span style=\"font-size: 15px;\">\u7528\u9014<\/span><\/strong><span style=\"font-size: 15px;\">\uff1a\u5728\u65b9\u6cd5\u7b7e\u540d\u4e0a\u58f0\u660e\u53ef\u80fd\u4f1a\u629b\u51fa\u7684\u5f02\u5e38\u7c7b\u578b\u3002<\/span><\/p>\n<\/li>\n<li>\n<p><strong>\u8bed\u6cd5<\/strong>\uff1apublic void methodName() throws ExceptionName1, ExceptionName2, ...<\/p>\n<\/li>\n<\/ul>\n<section style=\"margin-top: 16px;\"><strong><span style=\"font-size: 20px;\">\u533a\u522b<\/span><\/strong><\/section>\n<ul data-v-md-line=\"13\" style=\"list-style-type: disc;\" class=\"list-paddingleft-1\">\n<li style=\"line-height: normal;\">\n<p style=\"line-height: normal;\"><span style=\"font-size: 15px;\"><strong>\u629b\u51fa\u5f02\u5e38 vs. \u58f0\u660e\u5f02\u5e38<\/strong>\uff1athrow\u7528\u4e8e\u5b9e\u9645\u629b\u51fa\u5f02\u5e38\uff0c\u800cthrows\u7528\u4e8e\u58f0\u660e\u53ef\u80fd\u4f1a\u629b\u51fa\u7684\u5f02\u5e38\u7c7b\u578b\u3002<\/span><\/p>\n<\/li>\n<li style=\"line-height: normal;\">\n<p style=\"line-height: normal;\"><strong><span style=\"font-size: 15px;\">\u4f5c\u7528\u65f6\u673a<\/span><\/strong><span style=\"font-size: 15px;\">\uff1athrow\u5728\u4ee3\u7801\u6267\u884c\u671f\u95f4\u629b\u51fa\u5f02\u5e38\uff0c\u800cthrows\u5728\u7f16\u8bd1\u65f6\u58f0\u660e\u5f02\u5e38\u3002<\/span><\/p>\n<\/li>\n<li style=\"line-height: normal;\">\n<p style=\"line-height: normal;\"><strong><span style=\"font-size: 15px;\">\u5f02\u5e38\u5904\u7406<\/span><\/strong><span style=\"font-size: 15px;\">\uff1athrow\u629b\u51fa\u7684\u5f02\u5e38\u5fc5\u987b\u5728\u5f53\u524d\u65b9\u6cd5\u6216\u4e0a\u5c42\u8c03\u7528\u6808\u4e2d\u88abcatch\u5904\u7406\uff0c\u5426\u5219\u7a0b\u5e8f\u5c06\u7ec8\u6b62\u3002throws\u58f0\u660e\u7684\u5f02\u5e38\u53ef\u4ee5\u88abcatch\u5904\u7406\uff0c\u4e5f\u53ef\u4ee5\u4e0d\u5904\u7406\uff0c\u7531\u8c03\u7528\u8005\u51b3\u5b9a\u3002<\/span><\/p>\n<\/li>\n<\/ul>\n<section style=\"margin-top: 16px;\"><strong><span style=\"font-size: 20px;\">\u793a\u4f8b<\/span><\/strong><\/section>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\" data-lang=\"java\"><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__comment\">\/\/ \u4f7f\u7528throw\u629b\u51fa\u5f02\u5e38<\/span><\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__function\"><span class=\"code-snippet__keyword\">public<\/span> <span class=\"code-snippet__keyword\">void<\/span> <span class=\"code-snippet__title\">checkAge<\/span><span class=\"code-snippet__params\">(<span class=\"code-snippet__keyword\">int<\/span> age)<\/span> <\/span>{<\/span><\/code><code><span class=\"code-snippet_outer\">    <span class=\"code-snippet__keyword\">if<\/span> (age &lt; <span class=\"code-snippet__number\">0<\/span>) {<\/span><\/code><code><span class=\"code-snippet_outer\">        <span class=\"code-snippet__keyword\">throw<\/span> <span class=\"code-snippet__keyword\">new<\/span> IllegalArgumentException(<span class=\"code-snippet__string\">\"Age cannot be negative\"<\/span>);<\/span><\/code><code><span class=\"code-snippet_outer\">    }<\/span><\/code><code><span class=\"code-snippet_outer\">}<\/span><\/code><code><span class=\"code-snippet_outer\"><\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__comment\">\/\/ \u4f7f\u7528throws\u58f0\u660e\u5f02\u5e38<\/span><\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__function\"><span class=\"code-snippet__keyword\">public<\/span> <span class=\"code-snippet__keyword\">void<\/span> <span class=\"code-snippet__title\">readFile<\/span><span class=\"code-snippet__params\">()<\/span> <span class=\"code-snippet__keyword\">throws<\/span> IOException <\/span>{<\/span><\/code><code><span class=\"code-snippet_outer\">    <span class=\"code-snippet__comment\">\/\/ \u8bfb\u53d6\u6587\u4ef6\u7684\u903b\u8f91<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">}<\/span><\/code><\/pre>\n<\/section>\n<section style=\"margin-top: 16px;\"><strong><span style=\"font-size: 20px;\">\u8c03\u7528\u793a\u4f8b<\/span><\/strong><\/section>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\" data-lang=\"php\"><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">try<\/span> {<\/span><\/code><code><span class=\"code-snippet_outer\">    checkAge(<span class=\"code-snippet__number\">-1<\/span>);  <span class=\"code-snippet__comment\">\/\/ \u629b\u51faIllegalArgumentException<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">} <span class=\"code-snippet__keyword\">catch<\/span> (IllegalArgumentException e) {<\/span><\/code><code><span class=\"code-snippet_outer\">    <span class=\"code-snippet__comment\">\/\/ \u5904\u7406\u5f02\u5e38<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">}<\/span><\/code><code><span class=\"code-snippet_outer\"><\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">try<\/span> {<\/span><\/code><code><span class=\"code-snippet_outer\">    readFile();  <span class=\"code-snippet__comment\">\/\/ \u53ef\u80fd\u4f1a\u629b\u51faIOException<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">} <span class=\"code-snippet__keyword\">catch<\/span> (IOException e) {<\/span><\/code><code><span class=\"code-snippet_outer\">    <span class=\"code-snippet__comment\">\/\/ \u5904\u7406\u5f02\u5e38<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">} <span class=\"code-snippet__keyword\">catch<\/span> (<span class=\"code-snippet__keyword\">Exception<\/span> e) {<\/span><\/code><code><span class=\"code-snippet_outer\">    <span class=\"code-snippet__comment\">\/\/ \u5904\u7406\u5176\u4ed6\u7c7b\u578b\u7684\u5f02\u5e38<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">}<\/span><\/code><\/pre>\n<\/section>\n<p data-v-md-line=\"51\" style=\"line-height: normal;\"><span style=\"font-size: 15px;\">\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff1a<\/span><\/p>\n<ul data-v-md-line=\"53\" style=\"list-style-type: disc;\" class=\"list-paddingleft-1\">\n<li style=\"line-height: normal;\">\n<p style=\"line-height: normal;\"><span style=\"font-size: 15px;\">checkAge\u65b9\u6cd5\u4f7f\u7528throw\u629b\u51faIllegalArgumentException\uff0c\u8c03\u7528\u8005\u5fc5\u987b\u5728try-catch\u5757\u4e2d\u5904\u7406\u8be5\u5f02\u5e38\uff0c\u5426\u5219\u7a0b\u5e8f\u5c06\u7ec8\u6b62\u3002<\/span><\/p>\n<\/li>\n<li style=\"line-height: normal;\">\n<p style=\"line-height: normal;\"><span style=\"font-size: 15px;\">readFile\u65b9\u6cd5\u4f7f\u7528throws\u58f0\u660e\u53ef\u80fd\u4f1a\u629b\u51faIOException\uff0c\u8c03\u7528\u8005\u53ef\u4ee5\u9009\u62e9\u5728try-catch\u5757\u4e2d\u5904\u7406\u8be5\u5f02\u5e38\uff0c\u4e5f\u53ef\u4ee5\u4e0d\u5904\u7406\u3002<\/span><\/p>\n<\/li>\n<\/ul>\n<p style=\"display: none;\"><mp-style-type data-value=\"3\"><\/mp-style-type><\/p>\n<blockquote>\n<p>\u672c\u7bc7\u6587\u7ae0\u6765\u6e90\u4e8e\u5fae\u4fe1\u516c\u4f17\u53f7:                 \u4e92\u8054\u7f51\u9762\u8bd5\u5c0f\u5e2e\u624b              <\/p>\n<\/blockquote>\n<\/div>\n","raw":"","protected":false},"excerpt":{"rendered":"<p>throw\u5173\u952e\u5b57 \u7528\u9014\uff1a\u5728\u4ee3\u7801\u5757\u5185\u90e8\u663e\u5f0f\u5730\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u5bf9\u8c61\u3002 \u8bed\u6cd5\uff1athrow new ExceptionName(message &#8230;<\/p>\n","protected":false},"author":1,"featured_media":4097,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[30],"tags":[],"class_list":["post-4100","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-30"],"post_thumbnail_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145139984-150x150.jpeg","content_first_image":null,"post_medium_image_300":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145139984-300x128.jpeg","post_thumbnail_image_624":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145139984.jpeg","post_frist_image":null,"post_medium_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145139984-300x128.jpeg","post_large_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145139984.jpeg","post_full_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524145139984.jpeg","post_all_images":[],"videoAdId":"","listAd":"0","listAdId":"","listAdEvery":6,"total_comments":0,"category_name":"\u9762\u8bd5","post_date":"2024-04-19","like_count":"0","praiseWord":"\u9f13\u52b1","copyright_state":"","excitationAd":"0","rewardedVideoAdId":"","detailAdId":"","detailAd":"0","enterpriseMinapp":"0","audios":[],"postImageUrl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2023\/05\/20230519082947553.jpg","avatarurls":[],"related_posts":null,"pageviews":327,"next_post_id":4587,"next_post_title":"\u9762\u8bd5\u5b98\u6700\u6015\u4f60\u61c2\u7684Kafka\u9762\u8bd5\u9898\uff0c\u4e00\u62db\u81f4\u80dc\uff01","previous_post_id":4581,"previous_post_title":"Redis\u9ad8\u9891\u9762\u8bd5\u989850\u9053(\u4e8c)","_links":{"self":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/4100","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/comments?post=4100"}],"version-history":[{"count":0,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/4100\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media\/4097"}],"wp:attachment":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media?parent=4100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/categories?post=4100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/tags?post=4100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}