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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
02-26-2007, 10:05 PM
|
#1
|
Member
Registered: Mar 2005
Location: Kansas City
Distribution: Ubuntu 7.10
Posts: 47
Rep:
|
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?
|
|
|
02-27-2007, 04:36 PM
|
#2
|
Member
Registered: May 2002
Posts: 964
Rep:
|
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
|
|
|
All times are GMT -5. The time now is 05:51 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|