{"id":202542,"date":"2025-05-22T10:50:01","date_gmt":"2025-05-22T02:50:01","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202542\/"},"modified":"2025-05-22T10:50:01","modified_gmt":"2025-05-22T02:50:01","slug":"java-%e5%87%bd%e6%95%b0%e5%92%8c-java-%e6%96%b9%e6%b3%95%e7%9a%84%e9%94%99%e8%af%af%e5%a4%84%e7%90%86%e6%9c%ba%e5%88%b6","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202542\/","title":{"rendered":"Java \u51fd\u6570\u548c Java \u65b9\u6cd5\u7684\u9519\u8bef\u5904\u7406\u673a\u5236"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Java \u51fd\u6570\u548c Java \u65b9\u6cd5\u7684\u9519\u8bef\u5904\u7406\u673a\u5236<\/h1>\n<p><span style=\"font-size: 15px\">\u6709\u5fd7\u8005\uff0c\u4e8b\u7adf\u6210\uff01\u5982\u679c\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u90a3\u4e48\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aJava \u51fd\u6570\u548c Java \u65b9\u6cd5\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\u300b<\/span>\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff01\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/span><\/p>\n<p>Java \u51fd\u6570\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u9519\u8bef\uff0c\u800c\u65b9\u6cd5\u53ef\u4f7f\u7528\u8fd4\u56de\u7801\u6216\u5f02\u5e38\u3002Java \u51fd\u6570\u901a\u8fc7\u629b\u51fa\u5f02\u5e38\u5904\u7406\u9519\u8bef\uff0c\u8c03\u7528\u8005\u4f7f\u7528 try-catch \u5757\u6355\u83b7\u5e76\u5904\u7406\u5f02\u5e38\u3002Java \u65b9\u6cd5\u53ef\u8fd4\u56de\u6574\u6570\u8868\u793a\u64cd\u4f5c\u7ed3\u679c\uff0c\u6216\u629b\u51fa\u5f02\u5e38\u3002\u8d1f\u8fd4\u56de\u7801\u901a\u5e38\u8868\u793a\u9519\u8bef\uff0c\u800c\u6b63\u8fd4\u56de\u7801\u8868\u793a\u6210\u529f\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729916200671c6d287589b.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>Java \u51fd\u6570\u548c Java \u65b9\u6cd5\u7684\u9519\u8bef\u5904\u7406\u673a\u5236<\/strong><\/p>\n<p><strong>\u6982\u8ff0<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u51fd\u6570\u548c\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\u3002\u51fd\u6570\u4f7f\u7528\u5f02\u5e38\u6765\u5904\u7406\u9519\u8bef\uff0c\u800c\u65b9\u6cd5\u4f7f\u7528\u8fd4\u56de\u7801\u6216\u5f02\u5e38\u3002\u672c\u6587\u5c06\u8ba8\u8bba\u8fd9\u4e9b\u673a\u5236\u7684\u5de5\u4f5c\u539f\u7406\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4eec\u3002<\/p>\n<p><strong>Java \u51fd\u6570\u7684\u9519\u8bef\u5904\u7406<\/strong><\/p>\n<p>Java \u51fd\u6570\u4f7f\u7528\u5f02\u5e38\u6765\u5904\u7406\u9519\u8bef\u3002\u5f02\u5e38\u662f\u8868\u793a\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4\u53d1\u751f\u9519\u8bef\u7684\u7c7b\u3002\u5f53\u51fd\u6570\u68c0\u6d4b\u5230\u9519\u8bef\u65f6\uff0c\u5b83\u53ef\u4ee5\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<pre>public static int divide(int a, int b) throws ArithmeticException {\n  if (b == 0) {\n    throw new ArithmeticException(\"Division by zero\");\n  }\n  return a \/ b;\n}<\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c b \u7b49\u4e8e 0\uff0c\u51fd\u6570\u4f1a\u629b\u51fa ArithmeticException \u5f02\u5e38\u3002\u8c03\u7528\u8005\u53ef\u4ee5\u4f7f\u7528 try-catch \u5757\u6765\u6355\u83b7\u5f02\u5e38\u5e76\u5904\u7406\u9519\u8bef\u3002<\/p>\n<pre>try {\n  int result = divide(10, 0);\n} catch (ArithmeticException e) {\n  System.out.println(\"Division by zero error: \" + e.getMessage());\n}<\/pre>\n<p><strong>Java \u65b9\u6cd5\u7684\u9519\u8bef\u5904\u7406<\/strong><\/p>\n<p>Java \u65b9\u6cd5\u53ef\u4ee5\u4f7f\u7528\u4e24\u79cd\u673a\u5236\u6765\u5904\u7406\u9519\u8bef\uff1a<\/p>\n<ul>\n<li><strong>\u8fd4\u56de\u7801\uff1a<\/strong>\u65b9\u6cd5\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u8868\u793a\u64cd\u4f5c\u7ed3\u679c\u7684\u6574\u6570\u3002\u8d1f\u503c\u901a\u5e38\u8868\u793a\u9519\u8bef\u3002<\/li>\n<li><strong>\u5f02\u5e38\uff1a<\/strong>\u65b9\u6cd5\u53ef\u4ee5\u629b\u51fa\u5f02\u5e38\uff0c\u5c31\u50cf\u51fd\u6570\u4e00\u6837\u3002<\/li>\n<\/ul>\n<pre>public static int readFile(String filename) {\n  try {\n    \/\/ \u5c1d\u8bd5\u8bfb\u53d6\u6587\u4ef6\n  } catch (IOException e) {\n    return -1; \/\/ \u6587\u4ef6\u8bfb\u53d6\u5931\u8d25\n  }\n  return 0; \/\/ \u6587\u4ef6\u8bfb\u53d6\u6210\u529f\n}<\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>readFile<\/code> \u65b9\u6cd5\u8fd4\u56de 0 \u8868\u793a\u6587\u4ef6\u8bfb\u53d6\u6210\u529f\uff0c-1 \u8868\u793a\u5931\u8d25\u3002\u8c03\u7528\u8005\u53ef\u4ee5\u4f7f\u7528 if-else \u8bed\u53e5\u6216 switch \u8bed\u53e5\u6765\u5904\u7406\u8fd4\u56de\u503c\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 Java \u51fd\u6570\u548c Java \u65b9\u6cd5\u8fdb\u884c\u9519\u8bef\u5904\u7406\u7684\u5b8c\u6574\u793a\u4f8b\uff1a<\/p>\n<pre>import java.io.IOException;\n\npublic class ErrorHandlingExample {\n\n  public static void main(String[] args) {\n    try {\n      int result = divide(10, 2);\n      System.out.println(result);\n      int readFileResult = readFile(\"test.txt\");\n      if (readFileResult == 0) {\n        System.out.println(\"File read successfully\");\n      } else {\n        System.out.println(\"File read failed\");\n      }\n    } catch (ArithmeticException e) {\n      System.out.println(\"Division by zero error: \" + e.getMessage());\n    } catch (IOException e) {\n      System.out.println(\"File read error: \" + e.getMessage());\n    }\n  }\n\n  public static int divide(int a, int b) throws ArithmeticException {\n    if (b == 0) {\n      throw new ArithmeticException(\"Division by zero\");\n    }\n    return a \/ b;\n  }\n\n  public static int readFile(String filename) {\n    try {\n      \/\/ \u5c1d\u8bd5\u8bfb\u53d6\u6587\u4ef6\n    } catch (IOException e) {\n      return -1; \/\/ \u6587\u4ef6\u8bfb\u53d6\u5931\u8d25\n    }\n    return 0; \/\/ \u6587\u4ef6\u8bfb\u53d6\u6210\u529f\n  }\n}<\/pre>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aJava \u51fd\u6570\u548c Java \u65b9\u6cd5\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8ejava,\u9519\u8bef\u5904\u7406\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java \u51fd\u6570\u548c Java \u65b9\u6cd5&#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-202542","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202542","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=202542"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202542\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}