{"id":3541,"date":"2025-04-13T14:50:04","date_gmt":"2025-04-13T06:50:04","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3541"},"modified":"2025-04-13T14:50:04","modified_gmt":"2025-04-13T06:50:04","slug":"c-%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%ef%bc%88exception-handling%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/04\/13\/c-%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%ef%bc%88exception-handling%ef%bc%89\/","title":{"rendered":"C# \u5f02\u5e38\u5904\u7406\uff08Exception Handling\uff09"},"content":{"rendered":"\n<p>\u672c\u7ae0\u8282<strong>\u5168\u9762\u8be6\u7ec6\u7684\u4e86\u89e3 C# \u5f02\u5e38\u5904\u7406\uff08Exception Handling\uff09<\/strong>\uff0c\u5185\u5bb9\u6db5\u76d6\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u57fa\u7840\u6982\u5ff5\u4e0e\u7ed3\u6784<\/li>\n\n\n\n<li>\u5e38\u89c1\u5185\u7f6e\u5f02\u5e38\u7c7b\u578b<\/li>\n\n\n\n<li>\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b<\/li>\n\n\n\n<li>\u591a\u5c42\u5d4c\u5957\u4e0e\u5f02\u5e38\u4f20\u64ad\u673a\u5236<\/li>\n\n\n\n<li>\u5f02\u5e38\u8fc7\u6ee4\u5668\uff08C# 6+\uff09<\/li>\n\n\n\n<li>\u5f02\u5e38\u5904\u7406\u6700\u4f73\u5b9e\u8df5<\/li>\n\n\n\n<li>\u5b9e\u6218\u6848\u4f8b<\/li>\n\n\n\n<li>\u5b9e\u7528\u53c2\u8003\u94fe\u63a5<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001C# \u5f02\u5e38\u5904\u7406\u57fa\u7840\u7ed3\u6784<\/h2>\n\n\n\n<p>C# \u4f7f\u7528 <code>try-catch-finally<\/code> \u6765\u5904\u7406\u5f02\u5e38\uff0c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try\n{\n    \/\/ \u5c1d\u8bd5\u6267\u884c\u7684\u4ee3\u7801\uff08\u53ef\u80fd\u4f1a\u629b\u51fa\u5f02\u5e38\uff09\n}\ncatch (Exception ex)\n{\n    \/\/ \u5904\u7406\u5f02\u5e38\n}\nfinally\n{\n    \/\/ \u65e0\u8bba\u662f\u5426\u629b\u51fa\u5f02\u5e38\uff0c\u90fd\u4f1a\u6267\u884c\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u5e38\u89c1\u7684\u5185\u7f6e\u5f02\u5e38\u7c7b\u578b<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5f02\u5e38\u7c7b\u578b<\/th><th>\u63cf\u8ff0<\/th><\/tr><\/thead><tbody><tr><td><code>System.Exception<\/code><\/td><td>\u6240\u6709\u5f02\u5e38\u7684\u57fa\u7c7b<\/td><\/tr><tr><td><code>NullReferenceException<\/code><\/td><td>\u5bf9\u8c61\u672a\u5b9e\u4f8b\u5316<\/td><\/tr><tr><td><code>ArgumentException<\/code><\/td><td>\u53c2\u6570\u65e0\u6548<\/td><\/tr><tr><td><code>ArgumentNullException<\/code><\/td><td>\u53c2\u6570\u4e3a null<\/td><\/tr><tr><td><code>IndexOutOfRangeException<\/code><\/td><td>\u7d22\u5f15\u8d85\u51fa\u8303\u56f4<\/td><\/tr><tr><td><code>DivideByZeroException<\/code><\/td><td>\u9664\u6570\u4e3a\u96f6<\/td><\/tr><tr><td><code>FileNotFoundException<\/code><\/td><td>\u6587\u4ef6\u672a\u627e\u5230<\/td><\/tr><tr><td><code>InvalidOperationException<\/code><\/td><td>\u64cd\u4f5c\u4e0d\u5408\u6cd5<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\uff08\u7ee7\u627f Exception\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>public class MyCustomException : Exception\n{\n    public MyCustomException(string message) : base(message) { }\n\n    public override string ToString()\n    {\n        return $\"&#91;\u81ea\u5b9a\u4e49\u5f02\u5e38] {Message}\";\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>throw new MyCustomException(\"\u51fa\u73b0\u4e1a\u52a1\u903b\u8f91\u5f02\u5e38\");<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u5d4c\u5957\u5f02\u5e38\u4e0e\u5f02\u5e38\u4f20\u64ad<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>try\n{\n    DoSomething();\n}\ncatch (Exception ex)\n{\n    throw new Exception(\"\u5305\u88c5\u9519\u8bef\", ex); \/\/ \u5f02\u5e38\u94fe\n}<\/code><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u901a\u8fc7 <code>ex.InnerException<\/code> \u83b7\u53d6\u539f\u59cb\u9519\u8bef\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u5f02\u5e38\u8fc7\u6ee4\u5668\uff08C# 6.0+\uff09<\/h2>\n\n\n\n<p>\u6761\u4ef6\u6355\u83b7\u5f02\u5e38\u7684\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try\n{\n    throw new InvalidOperationException(\"\u9519\u8bef\uff01\");\n}\ncatch (Exception ex) when (ex.Message.Contains(\"\u9519\u8bef\"))\n{\n    Console.WriteLine(\"\u6355\u83b7\u5305\u542b\u2018\u9519\u8bef\u2019\u7684\u5f02\u5e38\");\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u5f02\u5e38\u5904\u7406\u6700\u4f73\u5b9e\u8df5<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5efa\u8bae<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>\u274c \u4e0d\u8981\u7528\u5f02\u5e38\u505a\u6d41\u7a0b\u63a7\u5236<\/td><td>\u7528 if-else \u5224\u65ad\u4ee3\u66ff\u5f02\u5e38<\/td><\/tr><tr><td>\u2705 \u5c3d\u91cf\u5177\u4f53\u5316\u6355\u83b7\u7c7b\u578b<\/td><td>\u5982\u6355\u83b7 <code>FileNotFoundException<\/code> \u800c\u975e\u6240\u6709\u5f02\u5e38<\/td><\/tr><tr><td>\u2705 \u4f7f\u7528 <code>using<\/code>\/<code>try-finally<\/code> \u91ca\u653e\u8d44\u6e90<\/td><td>\u4fdd\u8bc1\u8d44\u6e90\u91ca\u653e<\/td><\/tr><tr><td>\u2705 \u65e5\u5fd7\u8bb0\u5f55\u5f02\u5e38\u4fe1\u606f<\/td><td>\u5982\u8bb0\u5f55 <code>ex.Message<\/code> \u548c <code>ex.StackTrace<\/code><\/td><\/tr><tr><td>\u2705 \u4fdd\u6301\u5f02\u5e38\u539f\u59cb\u6808\u4fe1\u606f<\/td><td>\u7528 <code>throw;<\/code> \u800c\u975e <code>throw ex;<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e03\u3001\u5b9e\u6218\u6848\u4f8b<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u793a\u4f8b 1\uff1a\u5904\u7406\u6587\u4ef6\u8bfb\u5199\u5f02\u5e38<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>try\n{\n    string content = File.ReadAllText(\"data.txt\");\n    Console.WriteLine(content);\n}\ncatch (FileNotFoundException)\n{\n    Console.WriteLine(\"\u274c \u6587\u4ef6\u672a\u627e\u5230\uff01\");\n}\ncatch (IOException ex)\n{\n    Console.WriteLine($\"IO \u9519\u8bef\uff1a{ex.Message}\");\n}\nfinally\n{\n    Console.WriteLine(\"\u2705 \u6587\u4ef6\u8bfb\u53d6\u6d41\u7a0b\u7ed3\u675f\");\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u793a\u4f8b 2\uff1a\u4e1a\u52a1\u903b\u8f91\u4e2d\u629b\u51fa\u5f02\u5e38<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public void Withdraw(decimal amount)\n{\n    if (amount &lt;= 0)\n        throw new ArgumentException(\"\u91d1\u989d\u5fc5\u987b\u5927\u4e8e\u96f6\");\n    if (amount &gt; balance)\n        throw new InvalidOperationException(\"\u4f59\u989d\u4e0d\u8db3\");\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u793a\u4f8b 3\uff1a\u7edf\u4e00\u5f02\u5e38\u5904\u7406\uff08ASP.NET Core \u4e2d\u95f4\u4ef6\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>app.UseExceptionHandler(errorApp =&gt;\n{\n    errorApp.Run(async context =&gt;\n    {\n        var exceptionHandlerPathFeature = context.Features.Get&lt;IExceptionHandlerPathFeature&gt;();\n        var exception = exceptionHandlerPathFeature?.Error;\n\n        context.Response.ContentType = \"application\/json\";\n        await context.Response.WriteAsync(JsonSerializer.Serialize(new {\n            error = exception?.Message,\n            stack = exception?.StackTrace\n        }));\n    });\n});<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516b\u3001\u53c2\u8003\u6587\u6863\u4e0e\u94fe\u63a5<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/learn.microsoft.com\/zh-cn\/dotnet\/csharp\/language-reference\/keywords\/try-catch\">Microsoft \u5b98\u65b9\u6587\u6863\uff1atry-catch-finally \u8bed\u53e5<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/learn.microsoft.com\/zh-cn\/dotnet\/standard\/exceptions\/best-practices-for-exceptions\">Microsoft \u5b98\u65b9\u6587\u6863\uff1a\u5f02\u5e38\u5904\u7406\u6700\u4f73\u5b9e\u8df5<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.exception\">Exception \u7c7b\u7ed3\u6784\u56fe\uff08\u5b8c\u6574\u7ee7\u627f\u4f53\u7cfb\uff09<\/a><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\uff0c\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u7ae0\u8282\u5168\u9762\u8be6\u7ec6\u7684\u4e86\u89e3 C# \u5f02\u5e38\u5904\u7406\uff08Exception Handling\uff09\uff0c\u5185\u5bb9\u6db5\u76d6\uff1a \u4e00\u3001C# \u5f02\u5e38\u5904\u7406\u57fa [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3542,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83],"tags":[],"class_list":["post-3541","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3541","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3541"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3541\/revisions"}],"predecessor-version":[{"id":3543,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3541\/revisions\/3543"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3542"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}