{"id":1221,"date":"2025-01-25T23:18:31","date_gmt":"2025-01-25T15:18:31","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1221"},"modified":"2025-01-25T23:18:33","modified_gmt":"2025-01-25T15:18:33","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-c-%e4%b8%ad%e9%87%8d%e6%96%b0%e6%8a%9b%e5%87%ba%e5%bc%82%e5%b8%b8%e5%b9%b6%e4%bf%9d%e7%95%99%e5%a0%86%e6%a0%88%e8%b7%9f%e8%b8%aa%ef%bc%9a%e6%9c%80%e4%bd%b3%e5%ae%9e","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/25\/%e5%a6%82%e4%bd%95%e5%9c%a8-c-%e4%b8%ad%e9%87%8d%e6%96%b0%e6%8a%9b%e5%87%ba%e5%bc%82%e5%b8%b8%e5%b9%b6%e4%bf%9d%e7%95%99%e5%a0%86%e6%a0%88%e8%b7%9f%e8%b8%aa%ef%bc%9a%e6%9c%80%e4%bd%b3%e5%ae%9e\/","title":{"rendered":"\u5982\u4f55\u5728 C# \u4e2d\u91cd\u65b0\u629b\u51fa\u5f02\u5e38\u5e76\u4fdd\u7559\u5806\u6808\u8ddf\u8e2a\uff1a\u6700\u4f73\u5b9e\u8df5"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728 C# \u4e2d\u91cd\u65b0\u629b\u51fa\u5f02\u5e38\u65f6\uff0c\u4fdd\u7559\u5f02\u5e38\u7684\u5806\u6808\u8ddf\u8e2a\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u56e0\u4e3a\u5b83\u6709\u52a9\u4e8e\u8c03\u8bd5\u5e76\u8ffd\u8e2a\u5f02\u5e38\u7684\u6765\u6e90\u3002\u8981\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u4f60\u662f\u5e0c\u671b\u91cd\u65b0\u629b\u51fa\u539f\u59cb\u5f02\u5e38\uff0c\u8fd8\u662f\u5305\u88c5\u5b83\u6765\u63d0\u4f9b\u66f4\u591a\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u6709\u6548\u7684\u65b9\u6cd5\u6765\u4fdd\u7559\u5f02\u5e38\u5806\u6808\u8ddf\u8e2a\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u4f7f\u7528 <code>throw;<\/code> \u5173\u952e\u5b57\u91cd\u65b0\u629b\u51fa\u5f02\u5e38<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u4f60\u6355\u83b7\u4e00\u4e2a\u5f02\u5e38\u5e76\u5e0c\u671b\u91cd\u65b0\u629b\u51fa\u5b83\u65f6\uff0c\u76f4\u63a5\u4f7f\u7528 <code>throw;<\/code> \u5173\u952e\u5b57\uff0c\u800c\u4e0d\u662f\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 <code>throw new<\/code> \u5f02\u5e38\u3002\u8fd9\u4f1a\u4fdd\u7559\u539f\u59cb\u5f02\u5e38\u7684\u5806\u6808\u8ddf\u8e2a\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>try\n{\n    \/\/ \u6a21\u62df\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\n    throw new InvalidOperationException(\"Something went wrong.\");\n}\ncatch (Exception ex)\n{\n    \/\/ \u5904\u7406\u5f02\u5e38\u5e76\u91cd\u65b0\u629b\u51fa\n    Console.WriteLine($\"Caught exception: {ex.Message}\");\n    throw;  \/\/ \u91cd\u65b0\u629b\u51fa\u6355\u83b7\u7684\u5f02\u5e38\uff0c\u4fdd\u7559\u5806\u6808\u8ddf\u8e2a\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>throw;<\/code> \u4f1a\u91cd\u65b0\u629b\u51fa\u5f53\u524d\u6355\u83b7\u7684\u5f02\u5e38\uff0c\u5e76\u4fdd\u7559\u539f\u59cb\u7684\u5806\u6808\u8ddf\u8e2a\u4fe1\u606f\u3002\u8fd9\u662f\u6700\u5e38\u89c1\u548c\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5927\u90e8\u5206\u573a\u666f\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f18\u70b9\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4fdd\u7559\u539f\u59cb\u5f02\u5e38\u7684\u5b8c\u6574\u5806\u6808\u8ddf\u8e2a\uff0c\u4e0d\u4e22\u5931\u4efb\u4f55\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f3a\u70b9\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u65e0\u6cd5\u4fee\u6539\u5f02\u5e38\u6d88\u606f\u6216\u6dfb\u52a0\u66f4\u591a\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u4f7f\u7528 <code>throw new<\/code> \u5305\u88c5\u5f02\u5e38\u65f6\u4fdd\u7559\u5806\u6808\u8ddf\u8e2a<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u65f6\u5019\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b\u5728\u91cd\u65b0\u629b\u51fa\u5f02\u5e38\u65f6\uff0c\u6dfb\u52a0\u66f4\u591a\u7684\u4e0a\u4e0b\u6587\u6216\u4fe1\u606f\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u4ee5\u5c06\u539f\u59cb\u5f02\u5e38\u4f5c\u4e3a\u65b0\u5f02\u5e38\u7684\u5185\u90e8\u5f02\u5e38\uff08<code>InnerException<\/code>\uff09\u4f20\u9012\uff0c\u8fd9\u6837\u53ef\u4ee5\u5728\u65b0\u5f02\u5e38\u4e2d\u4fdd\u7559\u5806\u6808\u8ddf\u8e2a\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>try\n{\n    \/\/ \u6a21\u62df\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\n    throw new InvalidOperationException(\"Something went wrong.\");\n}\ncatch (Exception ex)\n{\n    \/\/ \u901a\u8fc7\u5305\u88c5\u539f\u59cb\u5f02\u5e38\uff0c\u4fdd\u7559\u5806\u6808\u4fe1\u606f\u5e76\u6dfb\u52a0\u66f4\u591a\u4e0a\u4e0b\u6587\n    throw new ApplicationException(\"An error occurred while processing your request.\", ex);\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u901a\u8fc7\u521b\u5efa\u65b0\u7684\u5f02\u5e38 <code>ApplicationException<\/code> \u5e76\u5c06\u539f\u59cb\u5f02\u5e38 <code>ex<\/code> \u4f5c\u4e3a <code>InnerException<\/code> \u4f20\u9012\uff0c\u6211\u4eec\u4fdd\u7559\u4e86\u539f\u59cb\u5f02\u5e38\u7684\u5806\u6808\u8ddf\u8e2a\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u6dfb\u52a0\u989d\u5916\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\uff08\u5982\u65b0\u7684\u9519\u8bef\u6d88\u606f\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f18\u70b9\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53ef\u4ee5\u5728\u629b\u51fa\u65b0\u5f02\u5e38\u65f6\u6dfb\u52a0\u66f4\u591a\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002<\/li>\n\n\n\n<li>\u901a\u8fc7 <code>InnerException<\/code> \u53ef\u4ee5\u8bbf\u95ee\u539f\u59cb\u5f02\u5e38\u53ca\u5176\u5806\u6808\u8ddf\u8e2a\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f3a\u70b9\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5806\u6808\u8ddf\u8e2a\u53ef\u80fd\u4f1a\u53d8\u5f97\u5197\u957f\uff0c\u56e0\u4e3a\u5b83\u4f1a\u5305\u542b\u591a\u4e2a\u5f02\u5e38\u94fe\u7684\u5806\u6808\u4fe1\u606f\uff0c\u53ef\u80fd\u4f7f\u8c03\u8bd5\u8fc7\u7a0b\u7a0d\u663e\u590d\u6742\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u4f7f\u7528 <code>Exception.Data<\/code> \u5c5e\u6027\u6dfb\u52a0\u81ea\u5b9a\u4e49\u4fe1\u606f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u53ea\u5e0c\u671b\u4e3a\u5f02\u5e38\u6dfb\u52a0\u66f4\u591a\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\uff0c\u800c\u4e0d\u4fee\u6539\u5f02\u5e38\u672c\u8eab\u7684\u7c7b\u578b\u6216\u5806\u6808\u8ddf\u8e2a\uff0c\u4f60\u53ef\u4ee5\u5229\u7528 <code>Exception.Data<\/code> \u5c5e\u6027\u5b58\u50a8\u81ea\u5b9a\u4e49\u7684\u952e\u503c\u5bf9\u3002\u8fd9\u4e0d\u4f1a\u5f71\u54cd\u5806\u6808\u8ddf\u8e2a\uff0c\u4f46\u53ef\u4ee5\u65b9\u4fbf\u5730\u4fdd\u5b58\u9644\u52a0\u4fe1\u606f\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>try\n{\n    \/\/ \u6a21\u62df\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\n    throw new InvalidOperationException(\"Something went wrong.\");\n}\ncatch (Exception ex)\n{\n    \/\/ \u5728\u5f02\u5e38\u7684 Data \u5c5e\u6027\u4e2d\u6dfb\u52a0\u81ea\u5b9a\u4e49\u4fe1\u606f\n    ex.Data&#91;\"AdditionalInfo\"] = \"Additional context about the error.\";\n\n    \/\/ \u91cd\u65b0\u629b\u51fa\u5f02\u5e38\uff0c\u4fdd\u7559\u5806\u6808\u8ddf\u8e2a\n    throw;\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u6355\u83b7\u5f02\u5e38\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 <code>ex.Data<\/code> \u5c5e\u6027\u6dfb\u52a0\u81ea\u5b9a\u4e49\u7684\u952e\u503c\u5bf9\u3002\u8fd9\u6837\uff0c\u5373\u4f7f\u6211\u4eec\u91cd\u65b0\u629b\u51fa\u5f02\u5e38\uff0c\u989d\u5916\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\u4e5f\u53ef\u4ee5\u901a\u8fc7 <code>Data<\/code> \u5c5e\u6027\u4fdd\u7559\u4e0b\u6765\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f18\u70b9\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e0d\u4f1a\u6539\u53d8\u5806\u6808\u8ddf\u8e2a\uff0c\u540c\u65f6\u80fd\u6dfb\u52a0\u9644\u52a0\u4fe1\u606f\u3002<\/li>\n\n\n\n<li>\u9002\u7528\u4e8e\u9700\u8981\u9644\u52a0\u4fe1\u606f\u800c\u4e0d\u5f71\u54cd\u5f02\u5e38\u7c7b\u578b\u7684\u573a\u666f\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f3a\u70b9\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9700\u8981\u901a\u8fc7 <code>Exception.Data<\/code> \u8bbf\u95ee\u9644\u52a0\u4fe1\u606f\uff0c\u8c03\u8bd5\u65f6\u53ef\u80fd\u4e0d\u5982 <code>InnerException<\/code> \u76f4\u89c2\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>throw;<\/code><\/strong> \u662f\u6700\u7b80\u5355\u4e14\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u91cd\u65b0\u629b\u51fa\u539f\u59cb\u5f02\u5e38\uff0c\u5e76\u4fdd\u7559\u5176\u5806\u6808\u8ddf\u8e2a\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u4f60\u9700\u8981\u4e3a\u5f02\u5e38\u6dfb\u52a0\u66f4\u591a\u4e0a\u4e0b\u6587\u4fe1\u606f\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>throw new<\/code> \u5305\u88c5\u539f\u59cb\u5f02\u5e38\uff0c\u5e76\u901a\u8fc7 <code>InnerException<\/code> \u4fdd\u7559\u5806\u6808\u8ddf\u8e2a\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u53ea\u9700\u8981\u9644\u52a0\u81ea\u5b9a\u4e49\u6570\u636e\u800c\u4e0d\u60f3\u6539\u53d8\u5f02\u5e38\u7c7b\u578b\u6216\u5806\u6808\u4fe1\u606f\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>Exception.Data<\/code> \u5c5e\u6027\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 C# \u4e2d\u91cd\u65b0\u629b\u51fa\u5f02\u5e38\u65f6\uff0c\u4fdd\u7559\u5f02\u5e38\u7684\u5806\u6808\u8ddf\u8e2a\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u56e0\u4e3a\u5b83\u6709\u52a9\u4e8e\u8c03\u8bd5\u5e76\u8ffd\u8e2a\u5f02\u5e38\u7684\u6765\u6e90\u3002\u8981\u505a\u5230\u8fd9\u4e00\u70b9 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[],"class_list":["post-1221","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1221","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=1221"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1221\/revisions"}],"predecessor-version":[{"id":1222,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1221\/revisions\/1222"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}