{"id":201771,"date":"2020-10-03T09:49:46","date_gmt":"2020-10-03T01:49:46","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=201771"},"modified":"2020-09-24T11:50:17","modified_gmt":"2020-09-24T03:50:17","slug":"learn-scala-exception","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/learn-scala-exception.html","title":{"rendered":"\u4e00\u8d77\u5b66Scala \u5f02\u5e38\u5904\u7406"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nScala \u662f\u4e00\u95e8\u591a\u8303\u5f0f\uff08multi-paradigm\uff09\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u8bbe\u8ba1\u521d\u8877\u662f\u8981\u96c6\u6210\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u548c\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u5404\u79cd\u7279\u6027\u3002
\nScala \u8fd0\u884c\u5728Java\u865a\u62df\u673a\u4e0a\uff0c\u5e76\u517c\u5bb9\u73b0\u6709\u7684Java\u7a0b\u5e8f\u3002
\nScala \u6e90\u4ee3\u7801\u88ab\u7f16\u8bd1\u6210Java\u5b57\u8282\u7801\uff0c\u6240\u4ee5\u5b83\u53ef\u4ee5\u8fd0\u884c\u4e8eJVM\u4e4b\u4e0a\uff0c\u5e76\u53ef\u4ee5\u8c03\u7528\u73b0\u6709\u7684Java\u7c7b\u5e93\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"
\nScala \u7684\u5f02\u5e38\u5904\u7406\u548c\u5176\u5b83\u8bed\u8a00\u6bd4\u5982 Java \u7c7b\u4f3c\u3002
\nScala \u7684\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u629b\u51fa\u5f02\u5e38\u7684\u65b9\u6cd5\u7684\u65b9\u5f0f\u6765\u7ec8\u6b62\u76f8\u5173\u4ee3\u7801\u7684\u8fd0\u884c\uff0c\u4e0d\u5fc5\u901a\u8fc7\u8fd4\u56de\u503c\u3002<\/p>\n

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

Scala \u629b\u51fa\u5f02\u5e38\u7684\u65b9\u6cd5\u548c Java\u4e00\u6837\uff0c\u4f7f\u7528 throw \u65b9\u6cd5\uff0c\u4f8b\u5982\uff0c\u629b\u51fa\u4e00\u4e2a\u65b0\u7684\u53c2\u6570\u5f02\u5e38\uff1a<\/p>\n

\r\nthrow new IllegalArgumentException<\/pre>\n
\u6355\u83b7\u5f02\u5e38<\/strong><\/div>\n

\u5f02\u5e38\u6355\u6349\u7684\u673a\u5236\u4e0e\u5176\u4ed6\u8bed\u8a00\u4e2d\u4e00\u6837\uff0c\u5982\u679c\u6709\u5f02\u5e38\u53d1\u751f\uff0ccatch\u5b57\u53e5\u662f\u6309\u6b21\u5e8f\u6355\u6349\u7684\u3002\u56e0\u6b64\uff0c\u5728catch\u5b57\u53e5\u4e2d\uff0c\u8d8a\u5177\u4f53\u7684\u5f02\u5e38\u8d8a\u8981\u9760\u524d\uff0c\u8d8a\u666e\u904d\u7684\u5f02\u5e38\u8d8a\u9760\u540e\u3002 \u5982\u679c\u629b\u51fa\u7684\u5f02\u5e38\u4e0d\u5728catch\u5b57\u53e5\u4e2d\uff0c\u8be5\u5f02\u5e38\u5219\u65e0\u6cd5\u5904\u7406\uff0c\u4f1a\u88ab\u5347\u7ea7\u5230\u8c03\u7528\u8005\u5904\u3002<\/p>\n

\u6355\u6349\u5f02\u5e38\u7684catch\u5b50\u53e5\uff0c\u8bed\u6cd5\u4e0e\u5176\u4ed6\u8bed\u8a00\u4e2d\u4e0d\u592a\u4e00\u6837\u3002\u5728Scala\u91cc\uff0c\u501f\u7528\u4e86\u6a21\u5f0f\u5339\u914d\u7684\u601d\u60f3\u6765\u505a\u5f02\u5e38\u7684\u5339\u914d\uff0c\u56e0\u6b64\uff0c\u5728catch\u7684\u4ee3\u7801\u91cc\uff0c\u662f\u4e00\u7cfb\u5217case\u5b57\u53e5\uff0c\u5982\u4e0b\u4f8b\u6240\u793a\uff1a<\/p>\n

\r\nimport java.io.FileReader\r\nimport java.io.FileNotFoundException\r\nimport java.io.IOException\r\n\r\nobject Test {\r\n   def main(args: Array[String]) {\r\n      try {\r\n         val f = new FileReader(\"input.txt\")\r\n      } catch {\r\n         case ex: FileNotFoundException =>{\r\n            println(\"Missing file exception\")\r\n         }\r\n         case ex: IOException => {\r\n            println(\"IO Exception\")\r\n         }\r\n      }\r\n   }\r\n}<\/pre>\n

\u6267\u884c\u4ee5\u4e0a\u4ee3\u7801\uff0c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n

\r\n$ scalac Test.scala \r\n$ scala Test\r\nMissing file exception<\/pre>\n

catch\u5b57\u53e5\u91cc\u7684\u5185\u5bb9\u8ddfmatch\u91cc\u7684case\u662f\u5b8c\u5168\u4e00\u6837\u7684\u3002\u7531\u4e8e\u5f02\u5e38\u6355\u6349\u662f\u6309\u6b21\u5e8f\uff0c\u5982\u679c\u6700\u666e\u904d\u7684\u5f02\u5e38\uff0cThrowable\uff0c\u5199\u5728\u6700\u524d\u9762\uff0c\u5219\u5728\u5b83\u540e\u9762\u7684case\u90fd\u6355\u6349\u4e0d\u5230\uff0c\u56e0\u6b64\u9700\u8981\u5c06\u5b83\u5199\u5728\u6700\u540e\u9762\u3002<\/p>\n

finally \u8bed\u53e5<\/strong><\/div>\n

finally \u8bed\u53e5\u7528\u4e8e\u6267\u884c\u4e0d\u7ba1\u662f\u6b63\u5e38\u5904\u7406\u8fd8\u662f\u6709\u5f02\u5e38\u53d1\u751f\u65f6\u90fd\u9700\u8981\u6267\u884c\u7684\u6b65\u9aa4\uff0c\u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n

\r\nimport java.io.FileReader\r\nimport java.io.FileNotFoundException\r\nimport java.io.IOException\r\n\r\nobject Test {\r\n   def main(args: Array[String]) {\r\n      try {\r\n         val f = new FileReader(\"input.txt\")\r\n      } catch {\r\n         case ex: FileNotFoundException => {\r\n            println(\"Missing file exception\")\r\n         }\r\n         case ex: IOException => {\r\n            println(\"IO Exception\")\r\n         }\r\n      } finally {\r\n         println(\"Exiting finally...\")\r\n      }\r\n   }\r\n}<\/pre>\n

\u6267\u884c\u4ee5\u4e0a\u4ee3\u7801\uff0c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n

\r\n$ scalac Test.scala \r\n$ scala Test\r\nMissing file exception\r\nExiting finally...<\/pre>\n","protected":false},"excerpt":{"rendered":"

Scala \u7684\u5f02\u5e38\u5904\u7406\u548c\u5176\u5b83\u8bed\u8a00\u6bd4\u5982 Java \u7c7b\u4f3c\u3002 Scala \u7684\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u629b\u51fa\u5f02\u5e38\u7684\u65b9\u6cd5\u7684\u65b9\u5f0f\u6765\u7ec8\u6b62\u76f8 […]<\/p>\n","protected":false},"author":1470,"featured_media":201759,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-201771","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/201771","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1470"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=201771"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/201771\/revisions"}],"predecessor-version":[{"id":201776,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/201771\/revisions\/201776"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/201759"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=201771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=201771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=201771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}