Error code

Error code

In 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 operating systems.


In programming languages without structured exception handling (e.g. in the C programming language), "error codes" are often stored in global variables with names such as 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:

/* attempt to open file for reading */FILE *fp = fopen("filename", "r"); /* if file cannot be opened, print error number and error string */if (fp = NULL){ printf("Cannot open file, error %d, %s ", errno, strerror(errno)); /* Alternatively one can use perror() to provide the above functionality */ perror("Cannot open file");}

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 multithreaded environment, since the process global variables could be set by more than one thread, causing a race condition. To fix this problem, POSIX defines errno to be a thread-local variable.

Error codes and exception handling

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.

A common coding error

If you're trying to run a fairly old program on systems with a recent libc, you can encounter the following situations depending on the version :

*The program runs but gives a warning

Incorrectly built binary which accesses errno or h_errno directly. Needs to be fixed.

*The program does not run giving error

symbol errno, version GLIBC_2.0 not defined in file with link time reference

errno is defined by the ISO C standard to be a modifiable lvalue of type int, and must not be explicitly declared.It was common in traditional C to declare errno manually (i.e., extern int errno) instead of including . It will not work with modern versions of the C library. However, on (very) old Unix systems, there may be no and the declaration is needed.

In such situations, the source code must be modified to replace all extern int errno lines with a single include: #include

ee also

*Alphanumerical List of Unix Error Codes and Messages
*Alphanumerical List of Error Codes and Messages used on Microsoft Windows based platforms
*Abort (computing)
*Aspect-oriented programming
*Static code analysis

External links

*Article " [ Errors: errno in UNIX programs] " by Chris Herborth
*Article " [ Exception Handling in C without C++] " by Tom Schotland and Peter Petersen
*Article " [ Error codes or Exceptions? Why is Reliable Software so Hard?] " by Damien Katz
* [ Win32 Error Handling Functions]
* [ Error Code Library]

Wikimedia Foundation. 2010.

Игры ⚽ Нужна курсовая?

Look at other dictionaries:

  • error code — UK US noun [countable] [singular error code plural error codes] computing a combination of letters and numbers on a computer screen that shows that there is a particular problem with a program Thesaurus: computer …   Useful english dictionary

  • error code — UK US noun [C] IT ► a number that appears on a computer screen to show that you have made a particular mistake or that something has gone wrong in a program: »Error code 7 keeps coming up on my monitor …   Financial and business terms

  • Error Code —   [engl.], Beendigungscode …   Universal-Lexikon

  • error code — UK / US noun [countable] Word forms error code : singular error code plural error codes computing a combination of letters and numbers on a computer screen that shows that there is a particular problem with a program …   English dictionary

  • error code — klaidos kodas statusas T sritis informatika apibrėžtis Programos aptiktos klaidos numeris. Paprastai programos vykdomoji dalis pateikia klaidos kodą, pagal kurį sąsajos dalis suformuoja ↑pranešimą apie klaidą. Programuotojas dirba su klaidos kodu …   Enciklopedinis kompiuterijos žodynas

  • Error-Code — Ein Fehlercode wird von Computerprogrammen erzeugt, wenn das Programm oder ein Unterprogramm auf Grund einer Fehlermeldung abgebrochen werden muss. Er besteht in der Regel aus einer Zahl, kann aber ebenso aus einer Zeichenfolge bestehen.… …   Deutsch Wikipedia

  • code — [kəʊd ǁ koʊd] noun 1. [countable] LAW a complete set of written rules or laws: • Each state in the US has a different criminal and civil code. ˈbuilding code [countable] LAW a set of rules that states what features a new building, bridge etc… …   Financial and business terms

  • Code Purple — can mean: a reference to the Air Quality Index used by the United States Environmental Protection Agency, when the Air Quality Index is between 201 and 300. Error Code Purple, an error message on Hewlett Packard personal computers. A Hospital… …   Wikipedia

  • Error — The word error has different meanings and usages relative to how it is conceptually applied. The concrete meaning of the Latin word error means wandering or straying . To the contrary of an illusion, an error or a mistake can sometimes be… …   Wikipedia

  • Error hiding — is an anti pattern, in computer programming. The programmer hides error messages by overriding them with exception handling. As a result of this the root error message is hidden from the user (hence error hiding ) and so they will not be told… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”