Printing Error Messages C
Contents |
C - Basic Syntax C - Data Types C - Variables C - Constants C - Storage Classes C - Operators C - Decision Making C - Loops C - Functions C - Scope Rules C - Arrays C - Pointers C - Strings C - Structures C c error handling best practices - Unions C - Bit Fields C - Typedef C - Input & Output C - File
Error.h C
I/O C - Preprocessors C - Header Files C - Type Casting C - Error Handling C - Recursion C - Variable Arguments C - c programming error codes Memory Management C - Command Line Arguments C Programming Resources C - Questions & Answers C - Quick Guide C - Useful Resources C - Discussion Selected Reading Developer's Best Practices Questions and Answers Effective Resume Writing HR Interview Questions Computer Glossary c stderr Who is Who C - Error Handling Advertisements Previous Page Next Page As such, C programming does not provide direct support for error handling but being a system programming language, it provides you access at lower level in the form of return values. Most of the C or even Unix function calls return -1 or NULL in case of any error and set an error code errno. It is set as a global variable and indicates an error occurred during any function call. You
Error Handling In C++
can find various error codes defined in
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 fprintf stderr c Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs strerror in c Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just
Print To Stderr C
like you, helping each other. Join them; it only takes a minute: Sign up Printing error messages up vote 3 down vote favorite 2 I am just wondering what is the best way to make custom print https://www.tutorialspoint.com/cprogramming/c_error_handling.htm error functions. For example I have some #defines like this in header file: #define SOCKET_ERR 0 #define BIND_ERR 1 #define LISTEN_ERR 2 etc Then maybe using this like this: if(/*something has gone wrong with socket*/) { print_error(SOCKET_ERR); } print_error(int error) { if(error == 0) { printf("Socket failure\n"); } } However, I don't think this perfect and want to do something much better. Maybe something a little bit more professional and maybe more scalable. Many thanks http://stackoverflow.com/questions/1679117/printing-error-messages for any advice, c share|improve this question asked Nov 5 '09 at 8:30 ant2009 78686247396 1 Use 'fprintf(stderr, ...)' to report errors (or, at least, normally write to 'stderr' rather than 'stdout' - or write to a log file, or both log file and stderr). –Jonathan Leffler Nov 5 '09 at 8:51 add a comment| 3 Answers 3 active oldest votes up vote 3 down vote accepted You might consider using variadic functions for error reporting, they become so much more versatile. For instance #include
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 http://stackoverflow.com/questions/385975/error-handling-in-c-code Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community http://www.cplusplus.com/reference/cstdio/perror/ of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Error handling in C code up vote 102 down vote favorite 59 What do you consider "best error handling practice" when it comes to error handling errors in a consistent way in a C library. There are 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 printing error messages 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 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,66513145 asked Dec 22 '08 at 10:46 Laserallan 6,71172956 add a comment| 17 Answers 17 active oldest votes up vote 51 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
optionally preceding it with the custom message specified in str. errno is an integral variable whose value describes the error condition or diagnostic information produced by a call to a library function (any function of the C standard library may set a value for errno, even if not explicitly specified in this reference, and even if no error happened), see errno for more info. The error message produced by perror is platform-depend. If the parameter str is not a null pointer, str is printed followed by a colon (:) and a space. Then, whether str was a null pointer or not, the generated error description is printed followed by a newline character ('\n'). perror should be called right after the error was produced, otherwise it can be overwritten by calls to other functions. Parameters. str C string containing a custom message to be printed before the error message itself. If it is a null pointer, no preceding custom message is printed, but the error message is still printed. By convention, the name of the application itself is generally used as parameter. Return Value none Example 1
2
3
4
5
6
7
8
9
10
11
12
13
/* perror example */ #include