{"id":243347,"date":"2022-06-02T08:27:43","date_gmt":"2022-06-02T00:27:43","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=243347"},"modified":"2022-05-23T14:29:28","modified_gmt":"2022-05-23T06:29:28","slug":"c-exception-handling","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/c-exception-handling.html","title":{"rendered":"\u4e86\u89e3\u4e0bC# \u5f02\u5e38\u5904\u7406"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5f02\u5e38\u662f\u5728\u7a0b\u5e8f\u6267\u884c\u671f\u95f4\u51fa\u73b0\u7684\u95ee\u9898\u3002C# \u4e2d\u7684\u5f02\u5e38\u662f\u5bf9\u7a0b\u5e8f\u8fd0\u884c\u65f6\u51fa\u73b0\u7684\u7279\u6b8a\u60c5\u51b5\u7684\u4e00\u79cd\u54cd\u5e94\uff0c\u6bd4\u5982\u5c1d\u8bd5\u9664\u4ee5\u96f6\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u5f02\u5e38\u63d0\u4f9b\u4e86\u4e00\u79cd\u628a\u7a0b\u5e8f\u63a7\u5236\u6743\u4ece\u67d0\u4e2a\u90e8\u5206\u8f6c\u79fb\u5230\u53e6\u4e00\u4e2a\u90e8\u5206\u7684\u65b9\u5f0f\u3002C# \u5f02\u5e38\u5904\u7406\u65f6\u5efa\u7acb\u5728\u56db\u4e2a\u5173\u952e\u8bcd\u4e4b\u4e0a\u7684\uff1atry\u3001catch\u3001finally \u548c throw\u3002<\/p>\n

    \n
  1. try\uff1a\u4e00\u4e2a try \u5757\u6807\u8bc6\u4e86\u4e00\u4e2a\u5c06\u88ab\u6fc0\u6d3b\u7684\u7279\u5b9a\u7684\u5f02\u5e38\u7684\u4ee3\u7801\u5757\u3002\u540e\u8ddf\u4e00\u4e2a\u6216\u591a\u4e2a catch \u5757\u3002<\/li>\n
  2. catch\uff1a\u7a0b\u5e8f\u901a\u8fc7\u5f02\u5e38\u5904\u7406\u7a0b\u5e8f\u6355\u83b7\u5f02\u5e38\u3002catch \u5173\u952e\u5b57\u8868\u793a\u5f02\u5e38\u7684\u6355\u83b7\u3002<\/li>\n
  3. finally\uff1afinally \u5757\u7528\u4e8e\u6267\u884c\u7ed9\u5b9a\u7684\u8bed\u53e5\uff0c\u4e0d\u7ba1\u5f02\u5e38\u662f\u5426\u88ab\u629b\u51fa\u90fd\u4f1a\u6267\u884c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u4e0d\u7ba1\u662f\u5426\u51fa\u73b0\u5f02\u5e38\u6587\u4ef6\u90fd\u8981\u88ab\u5173\u95ed\u3002<\/li>\n
  4. throw\uff1a\u5f53\u95ee\u9898\u51fa\u73b0\u65f6\uff0c\u7a0b\u5e8f\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u3002\u4f7f\u7528 throw \u5173\u952e\u5b57\u6765\u5b8c\u6210\u3002<\/li>\n<\/ol>\n
    \u8bed\u6cd5<\/strong><\/div>\n

    \u5047\u8bbe\u4e00\u4e2a\u5757\u5c06\u51fa\u73b0\u5f02\u5e38\uff0c\u4e00\u4e2a\u65b9\u6cd5\u4f7f\u7528 try \u548c catch \u5173\u952e\u5b57\u6355\u83b7\u5f02\u5e38\u3002try\/catch \u5757\u5185\u7684\u4ee3\u7801\u4e3a\u53d7\u4fdd\u62a4\u7684\u4ee3\u7801\uff0c\u4f7f\u7528 try\/catch \u8bed\u6cd5\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

    try\r\n{\r\n   \/\/ \u5f15\u8d77\u5f02\u5e38\u7684\u8bed\u53e5\r\n}\r\ncatch( ExceptionName e1 )\r\n{\r\n   \/\/ \u9519\u8bef\u5904\u7406\u4ee3\u7801\r\n}\r\ncatch( ExceptionName e2 )\r\n{\r\n   \/\/ \u9519\u8bef\u5904\u7406\u4ee3\u7801\r\n}\r\ncatch( ExceptionName eN )\r\n{\r\n   \/\/ \u9519\u8bef\u5904\u7406\u4ee3\u7801\r\n}\r\nfinally\r\n{\r\n   \/\/ \u8981\u6267\u884c\u7684\u8bed\u53e5\r\n}<\/pre>\n

    \u60a8\u53ef\u4ee5\u5217\u51fa\u591a\u4e2a catch \u8bed\u53e5\u6355\u83b7\u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38\uff0c\u4ee5\u9632 try \u5757\u5728\u4e0d\u540c\u7684\u60c5\u51b5\u4e0b\u751f\u6210\u591a\u4e2a\u5f02\u5e38\u3002<\/p>\n

    C# \u4e2d\u7684\u5f02\u5e38\u7c7b<\/strong><\/div>\n

    C# \u5f02\u5e38\u662f\u4f7f\u7528\u7c7b\u6765\u8868\u793a\u7684\u3002C# \u4e2d\u7684\u5f02\u5e38\u7c7b\u4e3b\u8981\u662f\u76f4\u63a5\u6216\u95f4\u63a5\u5730\u6d3e\u751f\u4e8e System.Exception \u7c7b\u3002System.ApplicationException \u548c System.SystemException \u7c7b\u662f\u6d3e\u751f\u4e8e System.Exception \u7c7b\u7684\u5f02\u5e38\u7c7b\u3002<\/p>\n

    System.ApplicationException \u7c7b\u652f\u6301\u7531\u5e94\u7528\u7a0b\u5e8f\u751f\u6210\u7684\u5f02\u5e38\u3002\u6240\u4ee5\u7a0b\u5e8f\u5458\u5b9a\u4e49\u7684\u5f02\u5e38\u90fd\u5e94\u6d3e\u751f\u81ea\u8be5\u7c7b\u3002<\/p>\n

    System.SystemException \u7c7b\u662f\u6240\u6709\u9884\u5b9a\u4e49\u7684\u7cfb\u7edf\u5f02\u5e38\u7684\u57fa\u7c7b\u3002<\/p>\n

    \u4e0b\u8868\u5217\u51fa\u4e86\u4e00\u4e9b\u6d3e\u751f\u81ea System.SystemException \u7c7b\u7684\u9884\u5b9a\u4e49\u7684\u5f02\u5e38\u7c7b\uff1a<\/p>\n\n\n\n\n\n\n\n\n\n\n\n
    \u5f02\u5e38\u7c7b<\/th>\n\u63cf\u8ff0<\/th>\n<\/tr>\n
    System.IO.IOException<\/td>\n\u5904\u7406 I\/O \u9519\u8bef\u3002<\/td>\n<\/tr>\n
    System.IndexOutOfRangeException<\/td>\n\u5904\u7406\u5f53\u65b9\u6cd5\u6307\u5411\u8d85\u51fa\u8303\u56f4\u7684\u6570\u7ec4\u7d22\u5f15\u65f6\u751f\u6210\u7684\u9519\u8bef\u3002<\/td>\n<\/tr>\n
    System.ArrayTypeMismatchException<\/td>\n\u5904\u7406\u5f53\u6570\u7ec4\u7c7b\u578b\u4e0d\u5339\u914d\u65f6\u751f\u6210\u7684\u9519\u8bef\u3002<\/td>\n<\/tr>\n
    System.NullReferenceException<\/td>\n\u5904\u7406\u5f53\u4f9d\u4ece\u4e00\u4e2a\u7a7a\u5bf9\u8c61\u65f6\u751f\u6210\u7684\u9519\u8bef\u3002<\/td>\n<\/tr>\n
    System.DivideByZeroException<\/td>\n\u5904\u7406\u5f53\u9664\u4ee5\u96f6\u65f6\u751f\u6210\u7684\u9519\u8bef\u3002<\/td>\n<\/tr>\n
    System.InvalidCastException<\/td>\n\u5904\u7406\u5728\u7c7b\u578b\u8f6c\u6362\u671f\u95f4\u751f\u6210\u7684\u9519\u8bef\u3002<\/td>\n<\/tr>\n
    System.OutOfMemoryException<\/td>\n\u5904\u7406\u7a7a\u95f2\u5185\u5b58\u4e0d\u8db3\u751f\u6210\u7684\u9519\u8bef\u3002<\/td>\n<\/tr>\n
    System.StackOverflowException<\/td>\n\u5904\u7406\u6808\u6ea2\u51fa\u751f\u6210\u7684\u9519\u8bef\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
    \u5f02\u5e38\u5904\u7406<\/strong><\/div>\n

    C# \u4ee5 try \u548c catch \u5757\u7684\u5f62\u5f0f\u63d0\u4f9b\u4e86\u4e00\u79cd\u7ed3\u6784\u5316\u7684\u5f02\u5e38\u5904\u7406\u65b9\u6848\u3002\u4f7f\u7528\u8fd9\u4e9b\u5757\uff0c\u628a\u6838\u5fc3\u7a0b\u5e8f\u8bed\u53e5\u4e0e\u9519\u8bef\u5904\u7406\u8bed\u53e5\u5206\u79bb\u5f00\u3002<\/p>\n

    \u8fd9\u4e9b\u9519\u8bef\u5904\u7406\u5757\u662f\u4f7f\u7528 try\u3001catch \u548c finally \u5173\u952e\u5b57\u5b9e\u73b0\u7684\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u5f53\u9664\u4ee5\u96f6\u65f6\u629b\u51fa\u5f02\u5e38\u7684\u5b9e\u4f8b\uff1a<\/p>\n

    \u5b9e\u4f8b<\/p>\n

    using System;\r\nnamespace ErrorHandlingApplication\r\n{\r\n    class DivNumbers\r\n    {\r\n        int result;\r\n        DivNumbers()\r\n        {\r\n            result = 0;\r\n        }\r\n        public void division(int num1, int num2)\r\n        {\r\n            try\r\n            {\r\n                result = num1 \/ num2;\r\n            }\r\n            catch (DivideByZeroException e)\r\n            {\r\n                Console.WriteLine(\"Exception caught: {0}\", e);\r\n            }\r\n            finally\r\n            {\r\n                Console.WriteLine(\"Result: {0}\", result);\r\n            }\r\n\r\n        }\r\n        static void Main(string[] args)\r\n        {\r\n            DivNumbers d = new DivNumbers();\r\n            d.division(25, 0);\r\n            Console.ReadKey();\r\n        }\r\n    }\r\n}<\/pre>\n

    \u5f53\u4e0a\u9762\u7684\u4ee3\u7801\u88ab\u7f16\u8bd1\u548c\u6267\u884c\u65f6\uff0c\u5b83\u4f1a\u4ea7\u751f\u4e0b\u5217\u7ed3\u679c\uff1a<\/p>\n

    Exception caught: System.DivideByZeroException: Attempted to divide by zero. \r\nat ...\r\nResult: 0<\/pre>\n
    \u521b\u5efa\u7528\u6237\u81ea\u5b9a\u4e49\u5f02\u5e38<\/strong><\/div>\n

    \u60a8\u4e5f\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5df1\u7684\u5f02\u5e38\u3002\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u5f02\u5e38\u7c7b\u662f\u6d3e\u751f\u81ea ApplicationException \u7c7b\u3002\u4e0b\u9762\u7684\u5b9e\u4f8b\u6f14\u793a\u4e86\u8fd9\u70b9\uff1a<\/p>\n

    \u5b9e\u4f8b<\/p>\n

    using System;\r\nnamespace UserDefinedException\r\n{\r\n   class TestTemperature\r\n   {\r\n      static void Main(string[] args)\r\n      {\r\n         Temperature temp = new Temperature();\r\n         try\r\n         {\r\n            temp.showTemp();\r\n         }\r\n         catch(TempIsZeroException e)\r\n         {\r\n            Console.WriteLine(\"TempIsZeroException: {0}\", e.Message);\r\n         }\r\n         Console.ReadKey();\r\n      }\r\n   }\r\n}\r\npublic class TempIsZeroException: ApplicationException\r\n{\r\n   public TempIsZeroException(string message): base(message)\r\n   {\r\n   }\r\n}\r\npublic class Temperature\r\n{\r\n   int temperature = 0;\r\n   public void showTemp()\r\n   {\r\n      if(temperature == 0)\r\n      {\r\n         throw (new TempIsZeroException(\"Zero Temperature found\"));\r\n      }\r\n      else\r\n      {\r\n         Console.WriteLine(\"Temperature: {0}\", temperature);\r\n      }\r\n   }\r\n}<\/pre>\n

    \u5f53\u4e0a\u9762\u7684\u4ee3\u7801\u88ab\u7f16\u8bd1\u548c\u6267\u884c\u65f6\uff0c\u5b83\u4f1a\u4ea7\u751f\u4e0b\u5217\u7ed3\u679c\uff1a<\/p>\n

    TempIsZeroException: Zero Temperature found<\/pre>\n
    \u629b\u51fa\u5bf9\u8c61<\/strong><\/div>\n

    \u5982\u679c\u5f02\u5e38\u662f\u76f4\u63a5\u6216\u95f4\u63a5\u6d3e\u751f\u81ea System.Exception \u7c7b\uff0c\u60a8\u53ef\u4ee5\u629b\u51fa\u4e00\u4e2a\u5bf9\u8c61\u3002\u60a8\u53ef\u4ee5\u5728 catch \u5757\u4e2d\u4f7f\u7528 throw \u8bed\u53e5\u6765\u629b\u51fa\u5f53\u524d\u7684\u5bf9\u8c61\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

    Catch(Exception e)\r\n{\r\n   ...\r\n   Throw e\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"

    \u5f02\u5e38\u63d0\u4f9b\u4e86\u4e00\u79cd\u628a\u7a0b\u5e8f\u63a7\u5236\u6743\u4ece\u67d0\u4e2a\u90e8\u5206\u8f6c\u79fb\u5230\u53e6\u4e00\u4e2a\u90e8\u5206\u7684\u65b9\u5f0f\u3002C# \u5f02\u5e38\u5904\u7406\u65f6\u5efa\u7acb\u5728\u56db\u4e2a\u5173\u952e\u8bcd\u4e4b\u4e0a\u7684\uff1atry\u3001c […]<\/p>\n","protected":false},"author":321,"featured_media":243350,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-243347","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\/243347","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\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=243347"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/243347\/revisions"}],"predecessor-version":[{"id":243355,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/243347\/revisions\/243355"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/243350"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=243347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=243347"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=243347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}