{"id":3058,"date":"2020-04-05T20:39:25","date_gmt":"2020-04-05T12:39:25","guid":{"rendered":"https:\/\/www.aoisnow.net\/blog\/?p=3058"},"modified":"2020-04-05T20:39:25","modified_gmt":"2020-04-05T12:39:25","slug":"tont-36683-ms-dos-%e6%98%af%e5%a6%82%e4%bd%95%e6%8a%a5%e5%91%8a%e9%94%99%e8%af%af%e4%bb%a3%e7%a0%81%e7%9a%84%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.aoisnow.net\/blog\/archives\/3058","title":{"rendered":"TONT 36683 MS-DOS \u662f\u5982\u4f55\u62a5\u544a\u9519\u8bef\u4ee3\u7801\u7684\uff1f"},"content":{"rendered":"<p>\u539f\u6587\u94fe\u63a5\uff1a<a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20050117-00\/?p=36683\" target=\"_blank\" rel=\"noopener\">https:\/\/devblogs.microsoft.com\/oldnewthing\/20050117-00\/?p=36683<\/a><\/p>\n<p>The old MS-DOS function calls (ah, int 21h), typically indicated error by returning with carry set and putting the error code in the AX register. These error codes will look awfully familiar today: They are the same error codes that Windows uses. All the small-valued error codes like ERROR_FILE_NOT_FOUND go back to MS-DOS (and possibly even further back).<\/p>\n<p>\u65e7\u5f0f\u7684 MS-DOS \u529f\u80fd\u8c03\u7528\uff08\u554a\uff0cINT 21h\uff09\u901a\u5e38\u901a\u8fc7\u5728\u8fd4\u56de\u4e2d\u8bbe\u7f6ecarry\u6807\u5fd7\u3001\u5e76\u5c06\u9519\u8bef\u4ee3\u7801\u653e\u5728AX\u5bc4\u5b58\u5668\u4e2d\u6765\u8868\u660e\u53d1\u751f\u4e86\u9519\u8bef\u3002\u8fd9\u4e9b\u9519\u8bef\u4ee3\u7801\u5373\u4f7f\u4eca\u5929\u770b\u8d77\u6765\u4e5f\u6781\u5176\u773c\u719f\uff0c\u56e0\u4e3a Windows \u4e5f\u4f7f\u7528\u4e86\u76f8\u540c\u7684\u9519\u8bef\u4ee3\u7801\u3002\u6240\u6709\u8fd9\u4e9b\u7531\u5c0f\u5c0f\u7684\u6570\u5b57\u4ee3\u8868\u7684\u9519\u8bef\u4ee3\u7801\u2014\u2014\u5982 ERROR_FILE_NOT_FOUND \u2014\u2014\u90fd\u53ef\u4ee5\u8ffd\u6eaf\u5230 MS-DOS\uff08\u5e76\u4e14\u53ef\u80fd\u66f4\u65e9\uff09\u3002<\/p>\n<p>Error code numbers are a major compatibility problem, because you cannot easily add new error code numbers without breaking existing programs. For example, it became well-known that \u201cThe only errors that can be returned from a failed call to OpenFile are 3 (path not found), 4 (too many open files), and 5 (access denied).\u201d If MS-DOS ever returned an error code not on that list, programs would crash because they used the error number as an index into a function table without doing a range check first. Returning a new error like 32 (sharing violation) meant that the programs would jump to a random address and die.<\/p>\n<p>\u9519\u8bef\u4ee3\u7801\u662f\u4e00\u9879\u4e3b\u8981\u7684\u517c\u5bb9\u6027\u95ee\u9898\uff0c\u56e0\u4e3a\u4f60\u65e0\u6cd5\u7b80\u5355\u5730\u589e\u52a0\u65b0\u7684\u9519\u8bef\u4ee3\u7801\uff0c\u800c\u4e0d\u5f71\u54cd\u5df2\u6709\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u4f8b\u5982\uff0c\u5e7f\u4e3a\u4eba\u77e5\u7684\u662f\u300e\u8c03\u7528 OpenFile \u4e14\u5931\u8d25\u65f6\uff0c\u53ef\u80fd\u7684\u8fd4\u56de\u53ea\u4f1a\u662f3\uff08\u627e\u4e0d\u5230\u8def\u5f84\uff09\u30014\uff08\u6253\u5f00\u7684\u6587\u4ef6\u6570\u5df2\u8d85\u51fa\u4e0a\u9650\uff09\u6216\u80055\uff08\u62d2\u7edd\u8bbf\u95ee\uff09\u300f\u3002\u5982\u679c MS-DOS \u8fd4\u56de\u4e86\u4e00\u4e2a\u4e0d\u5728\u8fd9\u4e2a\u5217\u8868\u4e0a\u7684\u9519\u8bef\u4ee3\u7801\uff0c\uff08\u7b2c\u4e09\u65b9\uff09\u7a0b\u5e8f\u4eec\u5c31\u4f1a\u5d29\u6e83\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u7a0b\u5e8f\u5c06\u9519\u8bef\u4ee3\u7801\u7528\u4f5c\u4e86\u51fd\u6570\u5217\u8868\u7684\u7d22\u5f15\uff0c\u751a\u81f3\u8fde\u8fb9\u754c\u68c0\u67e5\u90fd\u6ca1\u505a\u3002\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u9519\u8bef\u4ee3\u7801\uff08\u4f8b\u598232\uff09\u4f1a\u8ba9\u8fd9\u4e9b\u7a0b\u5e8f\u8df3\u5230\u4e00\u4e2a\u968f\u673a\u7684\u5730\u5740\uff0c\u7136\u540e\u70b8\u6389\u3002<\/p>\n<p>More about error number compatibility next time.<\/p>\n<p>\u4e0b\u6b21\u6709\u673a\u4f1a\u65f6\uff0c\u6211\u4eec\u518d\u6765\u8bf4\u6709\u5173\u9519\u8bef\u4ee3\u7801\u517c\u5bb9\u6027\u7684\u4e8b\u3002<\/p>\n<p>When it became necessary to add new error codes, compatibility demanded that the error codes returned by the functions not change. Therefore, if a new type of error occurred (for example, a sharing violation), one of the previous \u201cwell-known\u201d error codes was selected that had the most similar meaning and that was returned as the error code. (For \u201csharing violation\u201d, the best match is probably \u201caccess denied\u201d.) Programs which were \u201cin the know\u201d could call a new function called \u201cget extended error\u201d which returned one of the newfangled error codes (in this case, 32 for sharing violation).<\/p>\n<p>\u7b49\u5230\u589e\u52a0\u65b0\u7684\u9519\u8bef\u4ee3\u7801\u53d8\u5f97\u6709\u5fc5\u8981\u65f6\uff0c\u517c\u5bb9\u6027\u9700\u6c42\u4f1a\u8981\u6c42\u51fd\u6570\u8fd4\u56de\u7684\u9519\u8bef\u4ee3\u7801\u4e0d\u80fd\u6539\u53d8\u3002\u56e0\u6b64\uff0c\u5f53\u67d0\u4e2a\u65b0\u578b\u7684\u9519\u8bef\u53d1\u751f\u65f6\uff08\u4f8b\u5982\u5171\u4eab\u8fdd\u4f8b\uff09\uff0c\u4f1a\u8fd4\u56de\u4e00\u4e2a\u4e4b\u524d\u300e\u6700\u5e7f\u4e3a\u4eba\u77e5\u300f\u4e14\u542b\u4e49\u6700\u4e3a\u63a5\u8fd1\u7684\u7684\u9519\u8bef\u4ee3\u7801\u3002\uff08\u5bf9\u4e8e\u300e\u5171\u4eab\u8fdd\u4f8b\u300f\u6765\u8bf4\uff0c\u6700\u4f73\u7684\u5339\u914d\u9879\u662f\u300e\u62d2\u7edd\u8bbf\u95ee\u300f\uff09\u3002\u90a3\u4e9b\u300e\u77e5\u9053\u5185\u60c5\u300f\u7684\uff08\u65b0\uff09\u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u540d\u4e3a\u300e\u83b7\u53d6\u6269\u5c55\u9519\u8bef\u4ee3\u7801\u300f\u7684\u65b9\u6cd5\u6765\u83b7\u53d6\u90a3\u4e9b\u300e\u65b0\u5947\u300f\u7684\u9519\u8bef\u4ee3\u7801\uff08\u5728\u524d\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u83b7\u5f9732\u2014\u2014\u5171\u4eab\u8fdd\u4f8b\uff09\u3002<\/p>\n<p>The \u201cget extended error\u201d function returned other pieces of information. It gave you an \u201cerror class\u201d which gave you a vague idea of what type of problem it is (out of resources? physical media failure? system configuration error?), an \u201cerror locus\u201d which told you what type of device caused the problem (floppy? serial? memory?), and what I found to be the most interesting meta-information, the \u201csuggested action\u201d. Suggested actions were things like \u201cpause, then retry\u201d (for temporary conditions), \u201cask user to re-enter input\u201d (for example, file not found), or even \u201cask user for remedial action\u201d (for example, check that the disk is properly inserted).<\/p>\n<p>\u8fd9\u4e2a\u300e\u83b7\u53d6\u6269\u5c55\u9519\u8bef\u4ee3\u7801\u300f\u65b9\u6cd5\u8fd8\u8fd4\u56de\u4e86\u5176\u5b83\u7684\u4fe1\u606f\uff0c\u5b83\u4f1a\u7ed9\u4f60\u8fd4\u56de\u4e00\u4e2a\u300e\u9519\u8bef\u7c7b\u300f\u6765\u901a\u77e5\u4f60\u5173\u4e8e\u95ee\u9898\u7684\u5927\u81f4\u7c7b\u522b\uff08\u8d44\u6e90\u4e0d\u8db3\uff1f\u5a92\u4f53\u786c\u4ef6\u635f\u574f\uff1f\u7cfb\u7edf\u8bbe\u7f6e\u51fa\u9519\uff1f\uff09\uff0c\u4e00\u4e2a\u300e\u9519\u8bef\u6838\u5fc3\u300f\u6765\u544a\u77e5\u4f60\u5bfc\u81f4\u9519\u8bef\u53d1\u751f\u7684\u5177\u4f53\u8bbe\u5907\u7c7b\u578b\uff08\u8f6f\u9a71\uff1f\u4e32\u53e3\uff1f\u5185\u5b58\uff1f\uff09\uff0c\u4ee5\u53ca\u6211\u8ba4\u4e3a\u6700\u6709\u8da3\u7684\u5143\u4fe1\u606f\u90e8\u5206\u2014\u2014\u300e\u5efa\u8bae\u64cd\u4f5c\u300f\u3002\u300e\u5efa\u8bae\u64cd\u4f5c\u300f\u4f1a\u662f\u7c7b\u4f3c\u300e\u6682\u505c\uff0c\u7136\u540e\u91cd\u8bd5\u300f\uff08\u5bf9\u4e8e\u6682\u65f6\u6027\u7684\u95ee\u9898\u6765\u8bf4\uff09\uff0c\u300e\u8981\u6c42\u7528\u6237\u91cd\u65b0\u63d0\u4f9b\u8f93\u5165\u300f\uff08\u4f8b\u5982\u627e\u4e0d\u5230\u6587\u4ef6\u8fd9\u7c7b\u9519\u8bef\uff09\uff0c\u751a\u81f3\u300e\u8981\u6c42\u7528\u6237\u5b9e\u884c\u8865\u6551\u63aa\u65bd\u300f\uff08\u4f8b\u5982\u68c0\u67e5\u78c1\u76d8\u662f\u5426\u6b63\u786e\u63d2\u5165\u4e86\uff09\u7b49\u7b49\u3002<\/p>\n<p>The purpose of these meta-error values is to allow a program to recover when faced with an error code it doesn\u2019t understand. You could at least follow the meta-data to have an idea of what type of error it was (error class), where the error occurred (error locus), and what you probably should do in response to it (suggested action).<\/p>\n<p>\u8fd9\u4e9b\u6709\u5173\u9519\u8bef\u7684\u5143\u6570\u636e\u6709\u52a9\u4e8e\u7a0b\u5e8f\u5728\u9762\u5bf9\u4e00\u4e2a\u5176\u4e0d\u4e86\u89e3\u7684\u9519\u8bef\u4ee3\u7801\u65f6\uff0c\u4ece\u9519\u8bef\u4e2d\u6062\u590d\u8fc7\u6765\u3002\u81f3\u5c11\u4f60\u53ef\u4ee5\u4ece\u5143\u6570\u636e\u6240\u63cf\u8ff0\u4e2d\uff0c\u77e5\u6653\u51fa\u9519\u7684\u7c7b\u578b\uff08\u9519\u8bef\u7c7b\uff09\u3001\u51fa\u9519\u7684\u6240\u5728\uff08\u9519\u8bef\u6838\u5fc3\uff09\u4ee5\u53ca\u9762\u5bf9\u9519\u8bef\u65f6\u53ef\u80fd\u5e94\u8be5\u8fdb\u884c\u7684\u64cd\u4f5c\uff08\u5efa\u8bae\u64cd\u4f5c\uff09\u3002<\/p>\n<p>Sadly, this type of rich error information was lost when 16-bit programming was abandoned. Now you get an error code or an exception and you\u2019d better know what to do with it. For example, if you call some function and an error comes back, how do you know whether the error was a logic error in your program (using a handle after closing it, say) or was something that is externally-induced (for example, remote server timed out)? You don\u2019t.<\/p>\n<p>\u53ef\u60dc\u7684\u662f\uff0c\u8fd9\u79cd\u4e30\u5bcc\u7684\u9519\u8bef\u4fe1\u606f\u8bbe\u8ba1\u968f\u774016\u4f4d\u7a0b\u5e8f\u9000\u51fa\u5386\u53f2\u821e\u53f0\u88ab\u9057\u5f03\u4e86\u3002\u73b0\u5728\u5f53\u4f60\u9762\u5bf9\u9519\u8bef\u4ee3\u7801\u6216\u5f02\u5e38\u4fe1\u606f\u65f6\uff0c\u4f60\u6700\u597d\u77e5\u9053\u81ea\u5df1\u5e94\u8be5\u505a\u4ec0\u4e48\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u8c03\u7528\u4e86\u67d0\u4e2a\u65b9\u6cd5\uff0c\u7136\u540e\u8fd4\u56de\u4e86\u4e00\u4e2a\u9519\u8bef\uff0c\u4f60\u5982\u4f55\u77e5\u9053\u8fd9\u662f\u4f60\u7a0b\u5e8f\u8bbe\u8ba1\u4e2d\u7684\u903b\u8f91\u9519\u8bef\uff08\u4f8b\u5982\u5728\u5173\u95ed\u67d0\u4e2a\u53e5\u67c4\u540e\u53c8\u53bb\u4f7f\u7528\u5b83\uff09\uff0c\u8fd8\u662f\u67d0\u4e9b\u5916\u754c\u56e0\u7d20\u7684\u5bfc\u81f4\u7684\uff08\u4f8b\u5982\u8fdc\u7a0b\u670d\u52a1\u5668\u8d85\u65f6\uff09\uff1f\u4f60\u6ca1\u6cd5\u77e5\u9053\u3002<\/p>\n<p>This is particularly gruesome for exception-based programming. When you catch an exception, you can\u2019t tell by looking at it whether it\u2019s something that genuinely should crash the program (due to an internal logic error \u2013 a null reference exception, for example) or something that does not betray any error in your program but was caused externally (connection failed, file not found, sharing violation).<\/p>\n<p>\u8fd9\u79cd\u60c5\u5f62\u5728\u9762\u5bf9\u4ee5\u5f02\u5e38\u4e3a\u9519\u8bef\u673a\u5236\u7684\u7f16\u7a0b\u65f6\u5c24\u4e3a\u53ef\u60e7\u3002\u5f53\u4f60\u6355\u83b7\u4e86\u4e00\u4e2a\u5f02\u5e38\u65f6\uff0c\u4f60\u6ca1\u6709\u529e\u6cd5\u901a\u8fc7\u89c2\u5bdf\u5f02\u5e38\u4fe1\u606f\uff0c\u6765\u5224\u65ad\u662f\u4ec0\u4e48\u5730\u65b9\u771f\u7684\u8ba9\u4f60\u7684\u7a0b\u5e8f\u5d29\u6e83\u4e86\uff08\u6765\u81ea\u5185\u90e8\u7684\u903b\u8f91\u8bbe\u8ba1\u9519\u8bef\uff0c\u4f8b\u5982\u7a7a\u5f15\u7528\u5f02\u5e38\u7b49\u7b49\uff09\uff0c\u8fd8\u662f\u67d0\u4e9b\u5b9e\u9645\u4e0a\u4e0e\u4f60\u7684\u7a0b\u5e8f\u65e0\u5173\u3001\u800c\u662f\u67d0\u4e9b\u5916\u754c\u56e0\u7d20\u5bfc\u81f4\u7684\uff08\u4f8b\u5982\u8fde\u63a5\u5931\u8d25\u3001\u672a\u627e\u5230\u6587\u4ef6\u3001\u5171\u4eab\u8fdd\u4f8b\u7b49\u7b49\uff09\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u94fe\u63a5\uff1ahttps:\/\/devblogs.microsoft.com\/oldnewthing\/2005011 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-3058","post","type-post","status-publish","format-standard","hentry","category-tont_history"],"_links":{"self":[{"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/posts\/3058","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/comments?post=3058"}],"version-history":[{"count":0,"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/posts\/3058\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/media?parent=3058"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/categories?post=3058"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/tags?post=3058"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}