{"id":126,"date":"2013-02-10T05:37:12","date_gmt":"2013-02-09T21:37:12","guid":{"rendered":"http:\/\/bohu.net\/blog\/?p=126"},"modified":"2023-02-11T00:57:34","modified_gmt":"2023-02-10T16:57:34","slug":"net-windows-forms-c-%e7%ba%bf%e7%a8%8b%e4%b8%ad%e7%9a%84%e5%85%a8%e5%b1%80%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/as32.net\/blog\/126\/","title":{"rendered":".NET Windows Forms C# \u7ebf\u7a0b\u4e2d\u7684\u5168\u5c40\u5f02\u5e38\u5904\u7406"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-9546 size-medium\" src=\"https:\/\/bohu.net\/blog\/wp-content\/uploads\/2013\/02\/csharplogo-300x300-300x300.png\" alt=\"\" width=\"300\" height=\"300\" srcset=\"https:\/\/as32.net\/blog\/wp-content\/uploads\/2013\/02\/csharplogo-300x300.png 300w, https:\/\/as32.net\/blog\/wp-content\/uploads\/2013\/02\/csharplogo-300x300-150x150.png 150w\" sizes=\"auto, (max-width: 300px) 85vw, 300px\" \/><\/p>\n<h3>\u5f02\u5e38\u5904\u7406<\/h3>\n<p>\u4efb\u4f55\u7ebf\u7a0b\u521b\u5efa\u8303\u56f4\u5185try\/catch\/finally\u5757\uff0c\u5f53\u7ebf\u7a0b\u5f00\u59cb\u6267\u884c\u4fbf\u4e0d\u518d\u4e0e\u5176\u6709\u4efb\u4f55\u5173\u7cfb\u3002\u8003\u8651\u4e0b\u9762\u7684\u7a0b\u5e8f\uff1a<\/p>\n<pre class=\"brush:csharp\">public static void Main() {\ntry {\n       new Thread (Go).Start();\n}\ncatch (Exception ex) {\n       \/\/ \u4e0d\u4f1a\u5728\u8fd9\u5f97\u5230\u5f02\u5e38\n       Console.WriteLine (\"Exception!\");\n}\n\nstatic void Go() { throw null; }\n}<\/pre>\n<p>\u8fd9\u91cctry\/catch\u8bed\u53e5\u4e00\u70b9\u7528\u4e5f\u6ca1\u6709\uff0c\u65b0\u521b\u5efa\u7684\u7ebf\u7a0b\u5c06\u5f15\u53d1NullReferenceException\u5f02\u5e38\u3002\u5f53\u4f60\u8003\u8651\u5230\u6bcf\u4e2a\u7ebf\u7a0b\u6709\u72ec\u7acb\u7684\u6267\u884c\u8def\u5f84\u7684\u65f6\u5019\uff0c\u4fbf\u77e5\u9053\u8fd9\u884c\u4e3a\u662f\u6709\u9053\u7406\u7684\uff0c\u8865\u6551\u65b9\u6cd5\u662f\u5728\u7ebf\u7a0b\u5904\u7406\u7684\u65b9\u6cd5\u5185\u52a0\u5165\u4ed6\u4eec\u81ea\u5df1\u7684\u5f02\u5e38\u5904\u7406\uff1a<\/p>\n<pre class=\"brush:csharp\">public static void Main() {\n      new Thread (Go).Start();\n}\n\nstatic void Go() {\ntry {\n       ...\n       throw null;         \/\/ \u8fd9\u4e2a\u5f02\u5e38\u5728\u4e0b\u9762\u4f1a\u88ab\u6355\u6349\u5230\n       ...\n}\ncatch (Exception ex) {\n       \u8bb0\u5f55\u5f02\u5e38\u65e5\u5fd7\uff0c\u5e76\u4e14\u6216\u901a\u77e5\u53e6\u4e00\u4e2a\u7ebf\u7a0b\n       \u6211\u4eec\u53d1\u751f\u9519\u8bef\n       ...\n}<\/pre>\n<p>\u4ece.NET 2.0\u5f00\u59cb\uff0c\u4efb\u4f55\u7ebf\u7a0b\u5185\u7684\u672a\u5904\u7406\u7684\u5f02\u5e38\u90fd\u5c06\u5bfc\u81f4\u6574\u4e2a\u7a0b\u5e8f\u5173\u95ed\uff0c\u8fd9\u610f\u5473\u7740\u5ffd\u7565\u5f02\u5e38\u4e0d\u518d\u662f\u4e00\u4e2a\u9009\u9879\u4e86\u3002\u56e0\u6b64\u4e3a\u4e86\u907f\u514d\u7531\u672a\u5904\u7406\u5f02\u5e38\u5f15\u8d77\u7684\u7a0b\u5e8f\u5d29\u6e83\uff0ctry\/catch\u5757\u9700\u8981\u51fa\u73b0\u5728\u6bcf\u4e2a\u7ebf\u7a0b\u8fdb\u5165\u7684\u65b9\u6cd5\u5185\uff0c\u81f3\u5c11\u8981\u5728\u4ea7\u54c1\u7a0b\u5e8f\u4e2d\u5e94\u8be5\u5982\u6b64\u3002\u5bf9\u4e8e\u7ecf\u5e38\u4f7f\u7528\u201c\u5168\u5c40\u201d\u5f02\u5e38\u5904\u7406\u7684Windows Forms\u7a0b\u5e8f\u5458\u6765\u8bf4\uff0c\u8fd9\u53ef\u80fd\u6709\u70b9\u9ebb\u70e6\uff0c\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n<pre class=\"brush:csharp\">using System;\nusing System.Threading;\nusing System.Windows.Forms;\n\nstatic class Program {\nstatic void Main() {\n       Application.ThreadException += HandleError;\n       Application.Run (new MainForm());\n}\n\nstatic void HandleError (object sender, ThreadExceptionEventArgs e) {\n       \u8bb0\u5f55\u5f02\u5e38\u6216\u8005\u9000\u51fa\u7a0b\u5e8f\u6216\u8005\u7ee7\u7eed\u8fd0\u884c...\n}\n}<\/pre>\n<p>Application.ThreadException\u4e8b\u4ef6\u5728\u5f02\u5e38\u88ab\u629b\u51fa\u65f6\u89e6\u53d1\uff0c\u4ee5\u4e00\u4e2aWindows\u4fe1\u606f\uff08\u6bd4\u5982\uff1a\u952e\u76d8\uff0c\u9f20\u6807\u6d3b\u7740 &#8220;paint&#8221; \u7b49\u4fe1\u606f\uff09\u7684\u65b9\u5f0f\uff0c\u7b80\u8a00\u4e4b\uff0c\u4e00\u4e2aWindows Forms\u7a0b\u5e8f\u7684\u51e0\u4e4e\u6240\u6709\u4ee3\u7801\u3002\u867d\u7136\u8fd9\u770b\u8d77\u6765\u5f88\u5b8c\u7f8e\uff0c\u5b83\u4f7f\u4eba\u4ea7\u751f\u4e00\u79cd\u865a\u5047\u7684\u5b89\u5168\u611f\u2014\u2014\u6240\u6709\u7684\u5f02\u5e38\u90fd\u88ab\u4e2d\u592e\u5f02\u5e38\u5904\u7406\u6355\u6349\u5230\u4e86\u3002\u7531\u5de5\u4f5c\u7ebf\u7a0b\u629b\u51fa\u7684\u5f02\u5e38\u4fbf\u662f\u4e00\u4e2a\u6ca1\u6709\u88abApplication.ThreadException\u6355\u6349\u5230\u7684\u5f88\u597d\u7684\u4f8b\u5916\u3002\uff08\u5728Main\u65b9\u6cd5\u4e2d\u7684\u4ee3\u7801\uff0c\u5305\u62ec\u6784\u9020\u5668\u7684\u5f62\u5f0f\uff0c\u5728Windows\u4fe1\u606f\u5f00\u59cb\u524d\u5148\u6267\u884c\uff09<\/p>\n<p>.NET framework\u4e3a\u5168\u5c40\u5f02\u5e38\u5904\u7406\u63d0\u4f9b\u4e86\u4e00\u4e2a\u66f4\u4f4e\u7ea7\u522b\u7684\u4e8b\u4ef6\uff1aAppDomain.UnhandledException\uff0c\u8fd9\u4e2a\u4e8b\u4ef6\u5728\u4efb\u4f55\u7c7b\u578b\u7684\u7a0b\u5e8f\uff08\u6709\u6216\u6ca1\u6709\u7528\u6237\u754c\u9762\uff09\u7684\u4efb\u4f55\u7ebf\u7a0b\u6709\u4efb\u4f55\u672a\u5904\u7406\u7684\u5f02\u5e38\u89e6\u53d1\u3002\u5c3d\u7ba1\u5b83\u63d0\u4f9b\u4e86\u597d\u7684\u4e0d\u5f97\u5df2\u7684\u5f02\u5e38\u5904\u7406\u89e3\u51b3\u673a\u5236\uff0c\u4f46\u662f\u8fd9\u4e0d\u610f\u5473\u7740\u8fd9\u80fd\u4fdd\u8bc1\u7a0b\u5e8f\u4e0d\u5d29\u6e83\uff0c\u4e5f\u4e0d\u610f\u5473\u7740\u80fd\u53d6\u6d88.NET\u5f02\u5e38\u5bf9\u8bdd\u6846\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f02\u5e38\u5904\u7406 \u4efb\u4f55\u7ebf\u7a0b\u521b\u5efa\u8303\u56f4\u5185try\/catch\/finally\u5757\uff0c\u5f53\u7ebf\u7a0b\u5f00\u59cb\u6267\u884c\u4fbf\u4e0d\u518d\u4e0e\u5176\u6709\u4efb\u4f55\u5173\u7cfb\u3002\u8003\u8651\u4e0b\u9762 &hellip; <a href=\"https:\/\/as32.net\/blog\/126\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c.NET Windows Forms C# \u7ebf\u7a0b\u4e2d\u7684\u5168\u5c40\u5f02\u5e38\u5904\u7406\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":136,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,23],"tags":[50,98,419,459,465,516,532,585,616],"class_list":["post-126","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-net","category-23","tag-net","tag-c","tag-windows-forms","tag-459","tag-465","tag-516","tag-532","tag-585","tag-616"],"_links":{"self":[{"href":"https:\/\/as32.net\/blog\/wp-json\/wp\/v2\/posts\/126","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/as32.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/as32.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/as32.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/as32.net\/blog\/wp-json\/wp\/v2\/comments?post=126"}],"version-history":[{"count":1,"href":"https:\/\/as32.net\/blog\/wp-json\/wp\/v2\/posts\/126\/revisions"}],"predecessor-version":[{"id":9870,"href":"https:\/\/as32.net\/blog\/wp-json\/wp\/v2\/posts\/126\/revisions\/9870"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/as32.net\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/as32.net\/blog\/wp-json\/wp\/v2\/media?parent=126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/as32.net\/blog\/wp-json\/wp\/v2\/categories?post=126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/as32.net\/blog\/wp-json\/wp\/v2\/tags?post=126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}