Error code: Difference between revisions

Content deleted Content added
m fix broken link
Undid revision 1283414520 by 2600:387:9:5:0:0:0:72 (talk) unexplained removal of relevant wikilink
 
(239 intermediate revisions by more than 100 users not shown)
Line 1:
{{Short description|Code that indicates the nature of an error}}
In [[Programming language|computer programming]], '''error codes''' are enumerated messages that correspond to faults in a specific [[software application]]. They are typically used to identify faulty [[hardware]], [[software]], or incorrect user input in programming languages that lack [[exception handling]], although they are sometimes also be used in conjunction with exception handling. ''Error codes'' are not to be confused with ''return codes'', although both are commonly used together in error handling. Some of the most severe error codes visible to users are the "[[Blue Screen of Death]]" codes provided by the [[Microsoft Windows|Microsoft Windows operating systems]].
{{Distinguish|Error correction code}}
 
In [[computing]], an '''error code''' (or a '''return code''') is a numeric or [[Alphanumericals|alphanumeric]] code that indicates the nature of an error and, when possible, why it occurred.<ref>{{Cite web|url=https://www.computerhope.com/jargon/e/errorcode.htm|title=What is an Error Code?|website=ComputerHope.com |access-date=2020-01-22}}</ref> Error codes can be reported to end users of software, returned from [[Communication protocol|communication protocols]], or used within programs as a method of representing anomalous conditions.
In programming languages without structured exception handling (e.g. in the [[C programming language]]), ''error codes'' are often stored in [[global variable]]s with names like ''errno'' . Error codes are typically identified by number, each indicating a specific error condition. In an application that uses error codes, each function typically defines one ''return code'' to indicate a general failure. Upon receipt of that general failure return code, the programmer can check the value stored in the global error code to determine the condition that caused the function to fail. For example, to indicate that an attempt to open a file failed, a function may set the global error code to indicate the cause of the failure and return an invalid file handle. The following sample shows how the error code can be used to explain the cause of the error:
 
== In consumer products ==
/* attempt to open file for reading */
[[File:Error-code-e74.jpg|thumb|upright=1.3|alt=The error code E74. Above the large E 74 code is the message "System Error. Contact Xbox Customer Support." repeated in different languages.|Error E74 on the [[Xbox 360]], indicating a hardware failure<ref>{{Cite web |title=Xbox Support |url=https://support.xbox.com/en-US/help/errors/xbox-360/error-code-e74 |access-date=2023-03-12 |website=support.xbox.com}}</ref>]]
FILE *fp = fopen("filename", "r");
/* if file cannot be opened, print error number and error string */
if(fp == NULL)
printf("Cannot open file, error %i, %s\n", errno, strerror(errno));
 
Error codes are commonly encountered on displays of consumer electronics to users in order to communicate or specify an error. They can also be indicated by lights or beeps, e.g., if a device does not have a display. They are commonly reported by consumer electronics when users bring electronics to perform tasks that they cannot do (e.g., [[Division by zero|dividing by zero]]), or when the program within a device encounters an anomalous condition.
Since error codes are typically global variables, they can be read or written from any portion of the program. As with other global variables, that ease of access can be a source of problems in a [[thread (computer science)|multithreaded]] environment, since the process global variables could be set by more than one thread, causing a [[race condition]].
 
Error codes reported by consumer electronics are used to help diagnose and repair technical problems. An error code can be communicated to relevant support staff to identify potential fixes, or can simplify research into the cause of an error.
Error codes are slowly disappearing from the programmer's environment as modern [[object oriented]] [[computer languages]] replace them with [[exceptions]]. Exceptions have the advantage of being handled with explicit blocks of code, separate from the rest of the code. While it is considered poor practice in methodologies that use error codes and return codes to indicate failure, programmers often neglect to check return values for error conditions. That negligence can cause undesirable effects, as ignored error conditions often cause more severe problems later in the program. Exceptions are implemented in such a way as to separate the error handling code from the rest of the code. Separating the error handling code from the normal logic makes programs easier to write and understand, since one block of error handling code can service errors from any number of function calls. Exception handling also makes the code more readable than implementations with error codes, since exception handling does not disrupt the flow of the code with frequent checks for error conditions.
 
There is no definitive format for error codes, meaning that error codes typically differ from/between products and or companies.
[[Category:Programming bugs]]
 
[[category:programming]]
== In computer programming ==
Error codes in computers can be passed to the system itself, to judge how to respond to the error. Often error codes come synonymous with an [[Exit status|exit code]] or a return value. The system may also choose to pass the error code to its user(s). The [[Blue screen of death]] is an example of how the [[Microsoft Windows|Windows operating system]] communicates error codes to the user.
 
Error codes can be used within a computer program to represent an anomalous condition. A computer program can take different actions depending on the value of an error code.
 
Different programming languages, operating systems, and programming environments often have their own conventions and standards for the meanings and values of error codes. Examples include:
* [[Unix-like]] systems have an [[errno.h]] header file that contains the meanings and values of error codes returned by [[system call]]s and library functions.<ref>{{man|2|intro|v7}}</ref><ref>{{man|3|errno|Linux}}</ref><ref>{{man|2|intro|Solaris}}</ref>
* [[Windows API|Microsoft Windows' application programming interfaces (APIs)]] have several different standards for error code values, depending on the specific API being used.<ref>{{Cite web |title=[MS-ERREF]: Overview |url=https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/774ca55e-5e94-49ad-8710-c777fc717599 |access-date=2023-03-12 |website=learn.microsoft.com |date=30 March 2020 }}</ref>
 
The usage of error codes as an error handling strategy is often contrasted against using [[Exception handling|exceptions]] for error handling.<ref>{{Cite web |last=TylerMSFT |title=Modern C++ best practices for exceptions and error handling |url=https://learn.microsoft.com/en-us/cpp/cpp/errors-and-exception-handling-modern-cpp |access-date=2023-03-12 |website=Learn.Microsoft.com |date=17 October 2022 }}</ref><ref>{{Cite web |title=Standard C++ |url=https://isocpp.org/wiki/faq/exceptions#exceptions-avoid-spreading-out-error-logic |access-date=2023-03-12 |website=IsoCpp.org }}</ref>
 
==In communication protocols==
[[Communication protocol]]s typically define a standard set of error codes, as a means of communicating the status or result of an operation between the entities in the system.
 
Several high-level protocols in the [[TCP/IP stack]], such as [[HTTP]], [[File Transfer Protocol|FTP]], and [[Simple Mail Transfer Protocol|SMTP]], define their own standard sets of error codes:
* [[List of HTTP status codes]]
* [[List of FTP server return codes]]
* {{Section link|Simple Mail Transfer Protocol#Protocol overview}}
 
== In automobiles ==
Error codes in automobiles, sometimes referred to as trouble codes, indicate to a driver or car mechanic what is wrong with a vehicle before repairs are initiated.{{Citation needed|date=April 2023}}
 
In [[vehicle]]s with [[CAN bus]]es, error codes are often five-digit codes that pinpoint a particular car fault. Car owners can make use of an [[on-board diagnostics]] scanner or an owner's manual to identify the meaning of a trouble code. Five-digit diagnostic trouble codes typically consist of one letter and four numbers (e.g. P0123).{{Citation needed|date=April 2023}}
 
==See also==
* [[Abort (computing)]]
* [[category:Aspect-oriented programming]]
* [[Blue Screen of Death]]
* [[errno.h]], a header file in C that defines macros for reporting errors
* [[Exit status]]
* [[Failure]]
* [[HRESULT]], a computer programming [[data type]] used for error codes
* [[Static code analysis]]
 
==References==
{{Reflist}}
 
==External links==
* [http://msdn.microsoft.com/en-us/library/ms681381(VS.85).aspx Microsoft system error codes]
* [http://support.microsoft.com/kb/310123 Microsoft Device Manager error codes]
 
[[Category:Programming bugsSoftware]]