LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-26-2007, 10:05 PM   #1
Darkhack
Member
 
Registered: Mar 2005
Location: Kansas City
Distribution: Ubuntu 7.10
Posts: 47

Rep: Reputation: 15
Exception Handling (difference between strderr, exit, abort, return)


I'm a little confused on the way Linux (or UNIX in general) handles exception handling. I tried searching the forum and on Google for these different ways of exception handling. I was able to find out what they were but I couldn't seem to find a straight forward explanation on how they differed from each other and when to use one method over another. Thank you in advanced for your help.
  • What is the difference between terminating my program with return, exit, or abort?
  • Which should I use and when (if it matters)?
  • How should I use it? What is the "standard" way of doing doing it. Should I use exit(1), exit(-1), exit(EXIT_FAILURE), exit(EXIT_SUCCESS), or what?
  • How does strerr differ from stdout? From what I understand, by separating these two kinds of output I can control how it is used when I pipe it to another command or use cat on it. Are there any other uses?
 
Old 02-27-2007, 04:36 PM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
There are defacto standards. When a program runs with no error it should return a zero - exit(EXIT_SUCCESS) or return 0; These two accomplish the same thing - return (or return EXIT_SUCCESS) is normally found in main().

sysexits.h (if it is on your system) defines other exit codes that are standard.
Basically any positive number your shell can interpret means error return.
Exitinf from main() you can use return 1 or return EXIT_FAILURE; To exit in the middle of code call exit() with EXIT_FAILURE) or one of the sysexits.h codes

Bailing out with abort() usually causes a core dump which is messy for production but useful in testing. abort() sends a SIGABRT to the process.

There are three per-process file descriptors (in this case a FILE * struct pointing to a stream) defined for a process by default. They don't have to exist because you can close them (daemons do this ) but most times they are there:

stdin (input usually a tty),
stdout - the expected normal output stream,
stderr the standard error output stream.

Yes, in a decent UNIX console app like tr or col, you should expect that:
all error messages go to stderr,
any input data can be read from stdin, or from optional input files,
results go to stdout
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Exit with Return nopcoder Programming 0 12-27-2005 11:03 AM
Exception Handling vipinsharma Programming 2 09-25-2005 12:05 PM
Exception Handling C++ Hady Programming 3 07-27-2005 12:07 AM
Exception handling compilation errors ashwinipahuja Programming 6 07-03-2004 07:46 AM
python; exception handling TheLinuxDuck Programming 2 08-20-2003 03:01 PM

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

All times are GMT -5. The time now is 05:51 PM.

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