{"id":274,"date":"2022-06-27T14:32:04","date_gmt":"2022-06-27T06:32:04","guid":{"rendered":"https:\/\/www.fengjijiao.cn\/?p=274"},"modified":"2022-06-29T14:44:41","modified_gmt":"2022-06-29T06:44:41","slug":"csharp%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.fengjijiao.cn\/?p=274","title":{"rendered":"C#\u5f02\u5e38\u5904\u7406"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>using System;\r\n\r\nnamespace ConsoleApp1\r\n{\r\n    class Program\r\n    {\r\n        static void Main(string&#91;] args)\r\n        {\r\n            \/\/\u5f02\u5e38\u5904\u7406\r\n            \/\/\u5904\u7406\u65f6\u5efa\u7acb\u57284\u4e2a\u5173\u952e\u8bcd\u4e4b\u4e0a\uff1atry\u3001catch\u3001finally\u548cthrow\r\n            \/*\r\n             * try: \u4e00\u4e2atry\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\u4e2acatch\u5757\r\n             * catch: \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\r\n             * finally: finally\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\u4f60\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u4e0d\u7ba1\u662f\u5426\u51fa\u73b0\u5f02\u5e38\u6587\u4ef6\u90fd\u8981\u88ab\u5173\u95ed\r\n             * throw: \u5f53\u95ee\u9898\u51fa\u73b0\u65f6\uff0c\u7a0b\u5e8f\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u3002\u4f7f\u7528throw\u5173\u952e\u5b57\u6765\u5b8c\u6210\r\n             *\/\r\n             try\r\n            {\r\n                \/\/\u5f15\u8d77\u5f02\u5e38\u7684\u8bed\u53e5\r\n            }\r\n            catch(DivideByZeroException e1)\r\n            {\r\n                \/\/\u9519\u8bef\u5904\u7406\u4ee3\u7801\r\n            }\r\n            catch (Exception e1)\r\n            {\r\n                \/\/\u9519\u8bef\u5904\u7406\u4ee3\u7801\r\n            }\r\n            finally\r\n            {\r\n                \/\/\u8981\u6267\u884c\u7684\u91ca\u653e\u4ee3\u7801\r\n            }\r\n            \/\/\u5f02\u5e38\u7c7b\r\n            \/*\r\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\u4e8eSystem.Exception\u7c7b\u3002System.ApplicationException\u548cSystem.SystemException\u7c7b\u6d3e\u751f\u4e8eSystem.Exception\u7c7b\u7684\u5f02\u5e38\u7c7b\u3002\r\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\u6b64\u7c7b\u3002\r\n             * System.SystemException\u7c7b\u662f\u6240\u6709\u9884\u5b9a\u4e49\u7684\u7cfb\u7edf\u5f02\u5e38\u7684\u57fa\u7c7b\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u6d3e\u751f\u81eaSystem.SystemException\u7c7b\u7684\u9884\u5b9a\u4e49\u7684\u5f02\u5e38\u7c7b:\r\n             * \u5f02\u5e38\u7c7b          \u63cf\u8ff0\r\n             * System.IO.IOException            \u5904\u7406I\/O\u9519\u8bef\r\n             * System.IndexOutOfRangeException          \u5904\u7406\u5f53\u65b9\u6cd5\u6307\u5411\u8d85\u51fa\u8303\u56f4\u7684\u6570\u7ec4\u7d22\u5f15\u65f6\u751f\u6210\u7684\u9519\u8bef\r\n             * System.ArrayTypeMismatchException        \u5904\u7406\u5f53\u6570\u7ec4\u7c7b\u578b\u4e0d\u5339\u914d\u65f6\u751f\u6210\u7684\u9519\u8bef\r\n             * System.NullReferenceException            \u5904\u7406\u5f53\u4f9d\u4ece\u4e00\u4e2a\u7a7a\u5bf9\u8c61\u65f6\u751f\u6210\u7684\u9519\u8bef\r\n             * System.DivideByZeroException             \u5904\u7406\u5f53\u9664\u4ee50\u65f6\u751f\u6210\u7684\u9519\u8bef\r\n             * System.InvalidCastException              \u5904\u7406\u5728\u7c7b\u578b\u8f6c\u6362\u671f\u95f4\u751f\u6210\u7684\u9519\u8bef\r\n             * System.OutOfMemoryException              \u5904\u7406\u7a7a\u95f2\u5185\u5b58\u4e0d\u8db3\u751f\u6210\u7684\u9519\u8bef\r\n             * System.StackOverflowException            \u5904\u7406\u6808\u6ea2\u51fa\u751f\u6210\u7684\u9519\u8bef\r\n             *\/\r\n            \/\/\u5f02\u5e38\u5904\u7406\r\n            DivNumbers div1 = new DivNumbers();\r\n            div1.division(57, 0);\r\n\r\n            \/\/\u7528\u6237\u81ea\u5b9a\u4e49\u5f02\u5e38\uff0c\u6d3e\u751f\u81eaApplicationException\u7c7b\r\n            Temperature temp = new Temperature();\r\n            try\r\n            {\r\n                temp.showTemp();\r\n            }catch(TempIsZeroException e)\r\n            {\r\n                Console.WriteLine(\"TempIsZeroException: {0}\", e.Message);\r\n            }\r\n\r\n            \/\/\u629b\u51fa\u5bf9\u8c61\r\n            \/\/\u5982\u679c\u5f02\u5e38\u662f\u76f4\u63a5\u6216\u95f4\u63a5\u6d3e\u751f\u81eaSystem.Exception\u7c7b\uff0c\u4f60\u53ef\u4ee5\u629b\u51fa\u4e00\u4e2a\u5bf9\u8c61\u3002\u4f60\u53ef\u4ee5\u5728catch\u5757\u4e2d\u4f7f\u7528throw\u8bed\u53e5\u6765\u629b\u51fa\u5f53\u524d\u7684\u5bf9\u8c61\r\n            try\r\n            {\r\n                try\r\n                {\r\n                    int x1 = 9;\r\n                    int x2 = 0;\r\n                    int a = x1 \/ x2;\r\n                    Console.WriteLine(\"a: {0}\", a);\r\n                }\r\n                catch(DivideByZeroException e)\r\n                {\r\n                    throw e;\r\n                }\r\n            }\r\n            catch (DivideByZeroException e)\r\n            {\r\n                Console.WriteLine(\"top try...catch: {0}\", e);\r\n            }\r\n\r\n            Console.ReadKey();\r\n         }\r\n    }\r\n    public class TempIsZeroException: ApplicationException\r\n    {\r\n        public TempIsZeroException(string message) : base(message)\r\n        {\r\n\r\n        }\r\n    }\r\n    public 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            }else\r\n            {\r\n                Console.WriteLine(\"Temperature: {0}\", temperature);\r\n            }\r\n        }\r\n    }\r\n    public class DivNumbers\r\n    {\r\n        int result;\r\n        public 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    \r\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[19],"tags":[],"_links":{"self":[{"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=\/wp\/v2\/posts\/274"}],"collection":[{"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=274"}],"version-history":[{"count":1,"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=\/wp\/v2\/posts\/274\/revisions"}],"predecessor-version":[{"id":275,"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=\/wp\/v2\/posts\/274\/revisions\/275"}],"wp:attachment":[{"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}