LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-15-2015, 05:59 AM   #1
findnerd2
LQ Newbie
 
Registered: May 2015
Location: India
Posts: 11

Rep: Reputation: 0
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?
 
Old 06-15-2015, 06:56 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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:
  1. ALL code is just exactly "read-or-write", or "test-and-branch"
    1. You either read something and use it
    2. You could write something
    3. You could check/test something
    4. You make a branch decision based on your test
    5. However complex code may be, this is all that it is ever capable of doing at the most fundamental level
  2. 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
  3. 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
 
Old 06-15-2015, 07:34 AM   #3
findnerd2
LQ Newbie
 
Registered: May 2015
Location: India
Posts: 11

Original Poster
Rep: Reputation: 0
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;
}
 
Old 06-15-2015, 07:46 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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?
 
Old 06-15-2015, 05:27 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
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."
 
Old 06-17-2015, 05:48 AM   #6
findnerd2
LQ Newbie
 
Registered: May 2015
Location: India
Posts: 11

Original Poster
Rep: Reputation: 0
Thanks for help.
 
  


Reply

Tags
error, programing



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
carlhprogramming subbreddit, a different approach to programming lessons affinity Programming 0 01-15-2010 02:19 PM
How to approach programming printer output? skoona Programming 4 05-13-2004 08:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration