{"id":40128,"date":"2024-11-26T13:43:17","date_gmt":"2024-11-26T05:43:17","guid":{"rendered":"https:\/\/fwq.ai\/blog\/40128\/"},"modified":"2024-11-26T13:43:17","modified_gmt":"2024-11-26T05:43:17","slug":"%e6%80%8e%e4%b9%88%e6%8d%95%e6%8d%89windows%e9%94%99%e8%af%af","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/40128\/","title":{"rendered":"\u600e\u4e48\u6355\u6349windows\u9519\u8bef"},"content":{"rendered":"<blockquote><p>\n  \u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6355\u6349 windows \u9519\u8bef\uff1a1. \u4f7f\u7528 try-catch \u5757\u6355\u6349\u7279\u5b9a\u9519\u8bef\uff1b2. \u4f7f\u7528 getlasterror() \u51fd\u6570\u68c0\u7d22\u6700\u8fd1\u53d1\u751f\u7684\u9519\u8bef\u4ee3\u7801\uff1b3. \u4f7f\u7528 seterrormode() \u51fd\u6570\u8bbe\u7f6e\u7cfb\u7edf\u9519\u8bef\u5904\u7406\u65b9\u5f0f\uff1b4. \u4f7f\u7528 windows \u4e8b\u4ef6\u65e5\u5fd7\u8bb0\u5f55\u9519\u8bef\uff1b5. \u4f7f\u7528\u8c03\u8bd5\u5668\u8c03\u8bd5\u4ee3\u7801\u5e76\u6355\u6349\u9519\u8bef\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/09\/2024110905333924254.jpg\" class=\"aligncenter\" title=\"\u600e\u4e48\u6355\u6349windows\u9519\u8bef\u63d2\u56fe\" alt=\"\u600e\u4e48\u6355\u6349windows\u9519\u8bef\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u6355\u6349 Windows \u9519\u8bef<\/strong><\/p>\n<p>\u5728 Windows \u7cfb\u7edf\u4e2d\u6355\u6349\u9519\u8bef\u5bf9\u4e8e\u8c03\u8bd5\u548c\u4fee\u590d\u8f6f\u4ef6\u95ee\u9898\u81f3\u5173\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u6355\u6349\u9519\u8bef\u7684\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 try-catch \u5757<\/strong><\/p>\n<p>\u4f7f\u7528 try-catch \u5757\u662f\u6355\u6349\u4ee3\u7801\u4e2d\u9519\u8bef\u7684\u6700\u5e38\u89c1\u65b9\u6cd5\u3002\u5b83\u5141\u8bb8\u60a8\u6307\u5b9a\u5728\u53d1\u751f\u5f02\u5e38\u65f6\u6267\u884c\u7684\u4ee3\u7801\u5757\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 try-catch \u5757\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>try {\n  \/\/ \u4ee3\u7801\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\n} catch (Exception ex) {\n  \/\/ \u5904\u7406\u5f02\u5e38\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 GetLastError() \u51fd\u6570<\/strong><\/p>\n<p>GetLastError() \u51fd\u6570\u7528\u4e8e\u68c0\u7d22 Windows \u7cfb\u7edf\u4e2d\u6700\u8fd1\u53d1\u751f\u7684\u9519\u8bef\u4ee3\u7801\u3002\u60a8\u9700\u8981\u5305\u542b Windows.h \u5934\u6587\u4ef6\u624d\u80fd\u4f7f\u7528\u6b64\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 GetLastError() \u51fd\u6570\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>#include &lt;Windows.h&gt;\n\nint main() {\n  \/\/ \u4ee3\u7801\u53ef\u80fd\u5f15\u53d1\u9519\u8bef\n  DWORD error = GetLastError();\n  if (error != 0) {\n    \/\/ \u5904\u7406\u9519\u8bef\n  }\n  \n  return 0;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 SetErrorMode() \u51fd\u6570<\/strong><\/p>\n<p>SetErrorMode() \u51fd\u6570\u5141\u8bb8\u60a8\u8bbe\u7f6e Windows \u7cfb\u7edf\u5982\u4f55\u5904\u7406\u9519\u8bef\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u6b64\u51fd\u6570\u6765\u6291\u5236\u67d0\u4e9b\u9519\u8bef\u6216\u5c06\u9519\u8bef\u91cd\u5b9a\u5411\u5230\u81ea\u5b9a\u4e49\u5904\u7406\u7a0b\u5e8f\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 SetErrorMode() \u51fd\u6570\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>#include &lt;Windows.h&gt;\n\nint main() {\n  \/\/ \u8bbe\u7f6e Windows \u7cfb\u7edf\u6291\u5236\u6240\u6709\u9519\u8bef\n  SetErrorMode(SEM_FAILCRITICALERRORS);\n  \n  \/\/ \u4ee3\u7801\u53ef\u80fd\u5f15\u53d1\u9519\u8bef\n  \n  return 0;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528 Windows \u4e8b\u4ef6\u65e5\u5fd7<\/strong><\/p>\n<p>Windows \u4e8b\u4ef6\u65e5\u5fd7\u662f\u4e00\u4e2a\u7cfb\u7edf\u7ea7\u65e5\u5fd7\uff0c\u7528\u4e8e\u8bb0\u5f55\u5e94\u7528\u7a0b\u5e8f\u548c\u5176\u4ed6\u7ec4\u4ef6\u7684\u9519\u8bef\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4e8b\u4ef6\u67e5\u770b\u5668\u5de5\u5177\u8bbf\u95ee\u548c\u67e5\u770b\u4e8b\u4ef6\u65e5\u5fd7\u3002<\/p>\n<p><strong>5. \u4f7f\u7528\u8c03\u8bd5\u5668<\/strong><\/p>\n<p>\u8c03\u8bd5\u5668\u662f\u4e00\u4e2a\u5f3a\u5927\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u8c03\u8bd5\u4ee3\u7801\u5e76\u6355\u6349\u9519\u8bef\u3002Visual Studio \u7b49 IDE \u4e2d\u5185\u7f6e\u4e86\u8c03\u8bd5\u5668\u3002<\/p>\n<p>\u9009\u62e9\u6355\u6349\u9519\u8bef\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u60a8\u7684\u7279\u5b9a\u9700\u8981\u3002try-catch \u5757\u9002\u7528\u4e8e\u4ee3\u7801\u4e2d\u7684\u7279\u5b9a\u9519\u8bef\u5904\u7406\uff0c\u800c GetLastError() \u51fd\u6570\u5219\u9002\u7528\u4e8e\u4e00\u822c\u9519\u8bef\u5904\u7406\u3002SetErrorMode() \u51fd\u6570\u53ef\u7528\u4e8e\u6291\u5236\u6216\u91cd\u5b9a\u5411\u9519\u8bef\uff0c\u800c Windows \u4e8b\u4ef6\u65e5\u5fd7\u53ef\u7528\u4e8e\u8bb0\u5f55\u9519\u8bef\u5e76\u8fdb\u884c\u6545\u969c\u6392\u9664\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u6355\u6349\u9519\u8bef\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6355\u6349 windows \u9519\u8bef\uff1a1. \u4f7f\u7528 try-catch \u5757\u6355\u6349\u7279\u5b9a\u9519\u8bef\uff1b2. \u4f7f\u7528 getlasterror() \u51fd\u6570\u68c0\u7d22\u6700\u8fd1\u53d1\u751f\u7684\u9519\u8bef\u4ee3\u7801\uff1b3. \u4f7f\u7528 seterrormode() \u51fd\u6570\u8bbe\u7f6e\u7cfb\u7edf\u9519\u8bef\u5904\u7406\u65b9\u5f0f\uff1b4. \u4f7f\u7528 windows \u4e8b\u4ef6\u65e5\u5fd7\u8bb0\u5f55\u9519\u8bef\uff1b5. \u4f7f\u7528\u8c03\u8bd5\u5668\u8c03\u8bd5\u4ee3\u7801\u5e76\u6355\u6349\u9519\u8bef\u3002 \u5982\u4f55\u6355\u6349 Windows \u9519\u8bef \u5728 Windows \u7cfb\u7edf\u4e2d\u6355\u6349\u9519\u8bef\u5bf9\u4e8e\u8c03\u8bd5\u548c\u4fee\u590d\u8f6f\u4ef6\u95ee\u9898\u81f3\u5173\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u6355\u6349\u9519\u8bef\u7684\u65b9\u6cd5\uff1a 1. \u4f7f\u7528 try-catch \u5757 \u4f7f\u7528 try-catch \u5757\u662f\u6355\u6349\u4ee3\u7801\u4e2d\u9519\u8bef\u7684\u6700\u5e38\u89c1\u65b9\u6cd5\u3002\u5b83\u5141\u8bb8\u60a8\u6307\u5b9a\u5728\u53d1\u751f\u5f02\u5e38\u65f6\u6267\u884c\u7684\u4ee3\u7801\u5757\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 try-catch \u5757\u7684\u793a\u4f8b\uff1a try { \/\/ \u4ee3\u7801\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38 } catch (Exception ex) { \/\/ \u5904\u7406\u5f02\u5e38 } \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 GetLastError() \u51fd\u6570 GetLastError() \u51fd\u6570\u7528\u4e8e\u68c0\u7d22 Windows \u7cfb\u7edf\u4e2d\u6700\u8fd1\u53d1\u751f\u7684\u9519\u8bef\u4ee3\u7801\u3002\u60a8\u9700\u8981\u5305\u542b Windows.h \u5934\u6587\u4ef6\u624d\u80fd\u4f7f\u7528\u6b64\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 GetLastError() [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-40128","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=40128"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40128\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=40128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=40128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=40128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}