C Error Handling In
Contents |
known as exception handling). By convention, the programmer is expected to prevent errors from occurring in the first place, and test
C Error Handling Best Practices
return values from functions. For example, -1 and NULL are used c exception handling in several functions such as socket() (Unix socket programming) or malloc() respectively to indicate problems that
C Sharp Error Handling
the programmer should be aware about. In a worst case scenario where there is an unavoidable error and no way to recover from it, a C programmer c error handling errno usually tries to log the error and "gracefully" terminate the program. There is an external variable called "errno", accessible by the programs after including
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or
C Exit
posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss objective c error handling Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only
C Error Handling Goto
takes a minute: Sign up Error handling in C code up vote 102 down vote favorite 59 What do you consider "best practice" when it comes to error handling errors in a consistent way in a C library. There are https://en.wikibooks.org/wiki/C_Programming/Error_handling two ways I've been thinking of: Always return error code. A typical function would look like this: MYAPI_ERROR getObjectSize(MYAPIHandle h, int* returnedSize); The always provide an error pointer approach: int getObjectSize(MYAPIHandle h, MYAPI_ERROR* returnedError); When using the first approach it's possible to write code like this where the error handling check is directly placed on the function call: int size; if(getObjectSize(h, &size) != MYAPI_SUCCESS) { // Error handling } Which looks better than the error handling code here. MYAPIError error; int http://stackoverflow.com/questions/385975/error-handling-in-c-code size; size = getObjectSize(h, &error); if(error != MYAPI_SUCCESS) { // Error handling } However, I think using the return value for returning data makes the code more readable, It's obvious that something was written to the size variable in the second example. Do you have any ideas on why I should prefer any of those approaches or perhaps mix them or use something else? I'm not a fan of global error states since it tends to make multi threaded use of the library way more painful. EDIT: C++ specific ideas on this would also be interesting to hear about as long as they are not involving exceptions since it's not an option for me at the moment... c error-handling share|improve this question edited Nov 6 '13 at 19:09 ubershmekel 3,61513144 asked Dec 22 '08 at 10:46 Laserallan 6,70172956 add a comment| 17 Answers 17 active oldest votes up vote 49 down vote accepted I like the error as return-value way. If you're designing the api and you want to make use of your library as painless as possible think about these additions: store all possible error-states in one typedef'ed enum and use it in your lib. Don't just return ints or even worse, mix ints or different enumerations with return-codes. provide a function that converts errors into something human readable. Can be simple. Just error-enum in, const char* out. I know this idea makes multithreaded use a bit difficult, but
of a library call. The functions strerror and perror give you the standard error message for a given error code; the variable program_invocation_short_name gives you convenient access to the name of the program that http://www.gnu.org/s/libc/manual/html_node/Error-Messages.html encountered the error. Function: char * strerror (int errnum) Preliminary: | MT-Unsafe race:strerror | AS-Unsafe heap i18n | AC-Unsafe mem | See POSIX Safety Concepts. The strerror function maps the error code (see Checking for Errors) specified by the errnum argument to a descriptive error message string. The return value is a pointer to this string. The value errnum normally comes from the variable errno. You should not modify the error handling string returned by strerror. Also, if you make subsequent calls to strerror, the string might be overwritten. (But it’s guaranteed that no library function ever calls strerror behind your back.) The function strerror is declared in string.h. Function: char * strerror_r (int errnum, char *buf, size_t n) Preliminary: | MT-Safe | AS-Unsafe i18n | AC-Unsafe | See POSIX Safety Concepts. The strerror_r function works like strerror but instead of returning the c error handling error message in a statically allocated buffer shared by all threads in the process, it returns a private copy for the thread. This might be either some permanent global data or a message string in the user supplied buffer starting at buf with the length of n bytes. At most n characters are written (including the NUL byte) so it is up to the user to select a buffer large enough. This function should always be used in multi-threaded programs since there is no way to guarantee the string returned by strerror really belongs to the last call of the current thread. The function strerror_r is a GNU extension and it is declared in string.h. Function: void perror (const char *message) Preliminary: | MT-Safe race:stderr | AS-Unsafe corrupt i18n heap lock | AC-Unsafe corrupt lock mem fd | See POSIX Safety Concepts. This function prints an error message to the stream stderr; see Standard Streams. The orientation of stderr is not changed. If you call perror with a message that is either a null pointer or an empty string, perror just prints the error message corresponding to errno, adding a trailing newline. If you supply a non-null message argument, then perror prefixes its output with this string. It adds a