{"id":202576,"date":"2025-05-22T09:58:33","date_gmt":"2025-05-22T01:58:33","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202576\/"},"modified":"2025-05-22T09:58:33","modified_gmt":"2025-05-22T01:58:33","slug":"java%e5%87%bd%e6%95%b0%e5%8f%82%e6%95%b0%e5%92%8c%e8%bf%94%e5%9b%9e%e5%80%bc%e7%b1%bb%e5%9e%8b%e4%b8%8e%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202576\/","title":{"rendered":"Java\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u4e0e\u5f02\u5e38\u5904\u7406"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Java\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u4e0e\u5f02\u5e38\u5904\u7406<\/h1>\n<p>\u5728\u6587\u7ae0\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929\u5c31\u6574\u7406\u5206\u4eab\u300aJava\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u4e0e\u5f02\u5e38\u5904\u7406\u300b\uff0c\u804a\u804a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002<\/p>\n<p>\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u4ee5\u53ca\u5f02\u5e38\u5904\u7406\u51fd\u6570\u53c2\u6570\u7c7b\u578b\uff1a\u58f0\u660e\u63a5\u6536\u6570\u636e\u7684\u7c7b\u578b\uff0c\u5305\u62ec\u57fa\u672c\u7c7b\u578b\u548c\u5f15\u7528\u7c7b\u578b\u3002\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\uff1a\u58f0\u660e\u51fd\u6570\u8fd4\u56de\u6570\u636e\u7684\u7c7b\u578b\uff0c\u82e5\u4e0d\u8fd4\u56de\u6570\u636e\u5219\u4e3a void\u3002\u5f02\u5e38\u5904\u7406\uff1a\u4f7f\u7528 try-catch \u8bed\u53e5\u5904\u7406\u8fd0\u884c\u65f6\u9519\u8bef\uff0ctry \u5757\u5305\u542b\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\uff0ccatch \u5757\u5904\u7406\u7279\u5b9a\u5f02\u5e38\uff0cfinally \u5757\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\u90fd\u5c06\u6267\u884c\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729919374671c798e6e48f.jpg\" class=\"aligncenter\"><\/p>\n<h2>Java \u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u4e0e\u5f02\u5e38\u5904\u7406<\/h2>\n<p><strong>\u51fd\u6570\u53c2\u6570\u7c7b\u578b<\/strong><\/p>\n<p>\u51fd\u6570\u53c2\u6570\u7c7b\u578b\u58f0\u660e\u9700\u8981\u6307\u5b9a\u63a5\u6536\u7684\u53c2\u6570\u7684\u7c7b\u578b\u3002\u5728 Java \u4e2d\uff0c\u6709 8 \u79cd\u57fa\u672c\u7c7b\u578b\uff08<code>byte<\/code>\u3001<code>short<\/code>\u3001<code>int<\/code>\u3001<code>long<\/code>\u3001<code>float<\/code>\u3001<code>double<\/code>\u3001<code>char<\/code>\u3001<code>boolean<\/code>\uff09\u548c\u5f15\u7528\u7c7b\u578b\uff08\u5982 <code>String<\/code>\u3001<code>Integer<\/code>\uff09\u3002<\/p>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre>public static int add(int num1, int num2) {\n    return num1 + num2;\n}<\/pre>\n<p><strong>\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b<\/strong><\/p>\n<p>\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\u58f0\u660e\u4e86\u51fd\u6570\u8fd4\u56de\u7684\u503c\u7684\u7c7b\u578b\u3002\u5982\u679c\u51fd\u6570\u4e0d\u8fd4\u56de\u4efb\u4f55\u503c\uff0c\u5219\u8fd4\u56de\u503c\u7c7b\u578b\u4e3a <code>void<\/code>\u3002<\/p>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre>public static String greet(String name) {\n    return \"Hello, \" + name + \"!\";\n}<\/pre>\n<p><strong>\u5f02\u5e38\u5904\u7406<\/strong><\/p>\n<p>\u5f02\u5e38\u662f\u8fd0\u884c\u65f6\u9519\u8bef\uff0c\u5b83\u53ef\u4ee5\u4e2d\u65ad\u7a0b\u5e8f\u7684\u6b63\u5e38\u6267\u884c\u3002\u5728 Java \u4e2d\uff0c\u4f7f\u7528 <code>try-catch<\/code> \u8bed\u53e5\u6765\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<ul>\n<li><strong><code>try<\/code> \u5757\uff1a<\/strong> \u5305\u542b\u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801\u3002<\/li>\n<li><strong><code>catch<\/code> \u5757\uff1a<\/strong> \u5904\u7406\u7279\u5b9a\u7c7b\u578b\u7684\u5f02\u5e38\uff0c\u5e76\u5305\u542b\u5728\u51fa\u73b0\u5f02\u5e38\u65f6\u6267\u884c\u7684\u4ee3\u7801\u3002<\/li>\n<li><strong><code>finally<\/code> \u5757\uff08\u53ef\u9009\uff09\uff1a<\/strong> \u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0c\u90fd\u5c06\u6267\u884c\u7684\u4ee3\u7801\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre>public static void main(String[] args) {\n    try {\n        \/\/ \u53ef\u80fd\u53d1\u751f\u5f02\u5e38\u7684\u4ee3\u7801\n    } catch (ArithmeticException e) {\n        \/\/ \u5904\u7406\u7b97\u672f\u5f02\u5e38\u7684\u4ee3\u7801\n    } finally {\n        \/\/ \u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0c\u90fd\u4f1a\u6267\u884c\u7684\u4ee3\u7801\n    }\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p><strong>\u8ba1\u7b97\u4e24\u4e2a\u6570\u5b57\u7684\u603b\u548c\u5e76\u5904\u7406\u7b97\u672f\u5f02\u5e38\uff1a<\/strong><\/p>\n<pre>public class JavaFunctionsAndExceptions {\n    public static void main(String[] args) {\n        int num1 = 10;\n        int num2 = 0;\n\n        try {\n            \/\/ \u53ef\u80fd\u53d1\u751f\u7b97\u672f\u5f02\u5e38\u7684\u4ee3\u7801\n            int result = num1 \/ num2;  \/\/ \u9664\u6570\u4e3a 0 \u4f1a\u629b\u51fa\u7b97\u672f\u5f02\u5e38\n            System.out.println(\"Result: \" + result);\n        } catch (ArithmeticException e) {\n            \/\/ \u5904\u7406\u7b97\u672f\u5f02\u5e38\u7684\u4ee3\u7801\n            System.out.println(\"Error: Division by zero is not allowed.\");\n        } finally {\n            \/\/ \u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0c\u90fd\u4f1a\u6267\u884c\u7684\u4ee3\u7801\n            System.out.println(\"Done\");\n        }\n    }\n}<\/pre>\n<p>\u8fd0\u884c\u6b64\u4ee3\u7801\u5c06\u8f93\u51fa\uff1a<\/p>\n<pre>Error: Division by zero is not allowed.\nDone<\/pre>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aJava\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u4e0e\u5f02\u5e38\u5904\u7406\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u4e0e\u5f02&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-202576","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202576","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202576"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202576\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}