Java Developer ForumWhat is best approach for error handling in c programming?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Java Developer ForumWhat is best approach for error handling in c programming?
I am writing a program for error handling in c programming but I am confusing to choose best approach for handle error from if-else and switch-case. I have asked already other C developer forum but I am not satsfied from that.
Please reply what is the best for me?
First thing to do is to post what you do have. The link you provided appeared to just go to that other forum and didn't show your question you asked over there. So no one can really tell you an answer until they see more details about this error handing code you're trying to write. A description on the problem and proposed solution would be helpful. Code you have written up to this point would also be helpful. When posting code, please use [code][/code] tags so that the code will show up similar to this example:
Code:
#include <stdio.h>
int main(int argc, char **argv)
{
...
Some quick thoughts which perhaps may help by the way:
ALL code is just exactly "read-or-write", or "test-and-branch"
You either read something and use it
You could write something
You could check/test something
You make a branch decision based on your test
However complex code may be, this is all that it is ever capable of doing at the most fundamental level
If your if-else statement is overly complicated, you may need to simplify it by expanding it into several statements or breaking down the logic of it to something more simple, but anything you can write in pseudo-code as a statement of proposed coding actions, you can write as code
Switch-case is one special form of if-elseif-else, I personally use that when I have a large variety of test possibilities to apply to one variable I'm testing; such as once I get above 3 test possibilities, I may choose to use a switch statement. That is arbitrary and really based on my preference, so just a recommendation
What is best approach for error handling in c programming?
Hi,
I am writing this program and using if-else, But i am confusing, I want to know what is the best approach for handle error from if-else and switch-case.
Code:
include <stdio.h>
#include <errno.h>
#include <string.h>
extern int errno ;
int main ()
{
FILE * pf;
int errnum;
pf = fopen ("unexist.txt", "rb");
if (pf == NULL)
{
errnum = errno;
fprintf(stderr, "Value of errno: %d\n", errno);
perror("Error printed by perror");
fprintf(stderr, "Error opening file: %s\n", strerror( errnum ));
}
else
{
fclose (pf);
}
return 0;
}
First I offer an excellent "Well done!" because I approve of doing exactly the actions you performed:
"print out the errno number"
"print out the errno string using strerror()"
What exactly is your confusion?
"how to handle error from if-else"
If your confusion is "what you do in the event that if-else has a fault when you use it"? The answer is that it is not like a library call such as fopen() where you have a return and potential setting of errno as a result of an error when you call that function. If-else are not library functions which you call that can result in faults, instead they are fundamental syntax of the C language where the pre-processor and compiler will determine whether or not they have been coded syntactically correct. Beyond that, if your code statement is incorrect, or flawed, then you will get the wrong outcome. Same thing for the switch statement.
Is that what you're getting at when you say you're confused about how to handle faults for them?
Another strategy that is sometimes used, although not-so-much with "C," is to write low-level library routines that "throw an exception" if something goes wrong. In this way, client programs do not have to constantly check return-codes to see if something worked: "if we are still here, it worked."
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.