Post a Comment
about stderr...
Too bad that an introduction to error reporting in unix left out
a) fprintf(stderr,...)
and
b) the return value from main ...
We did *not* leave out E_OK or its equivalent in P1003 by accident. There's a _reason_ why 0 is the OK return value, and it has to do with a common C idiom that should be in every C programmer's vocabulary:
fd = open(...);
if (!fd) {
/* YOU FAILED */
}
which is more commonly used for functions that return a status and so don't need the assignment:
if (!somefunction()) {
/* YOU FAILED */
}
Why on earth did you use open for this example? It like most such functions returns -1 on error making it a fairly poor choice for discussing a lazy shorthand that doesn't even apply with its use.
The reason EOK was probably never introduced into POSIX is that errno is not defined to detect errors, but rather to report specific errors. The value of errno should only be used when a function specifically defined as setting errno in the event of some error condition signals that an error has occurred. This is typically done by that function returning -1.
... Yeah, sure, exception is much more powerful.
If you need fine grained error recovery, instead of doing:
int err;
..
if ((err = command(...)) < 0) {
/* Fine grained error recovery. */
err = errno;
goto out;
}
You do:
int previous_err = 0;
int err = 0;
...
if (!previous_err) {
try {
command(...);
}
catch (...) {
// Fine gained error recovery.
...
// No goto. We don't do such things in
// CPP. Lets us just do if previous_err
// 128 times till the end of the function.
previous_err = err;
}
}
<SARCASM>
Yep, indeed C++ exception is much better at fine-grained error recovery then C
is.
Thank God kernels and OS libraries are still written in C!
</SARCASM>
- Gilboa
* Sorry about that... I'm just faced, on a daily basis, by herd of C++ programmers who throw C++ on each stupid error, failing to do any type of error recovery and leaving all the clean up to the CPP/C libraries/drivers/etc.
Edited 2006-09-08 21:47
Awww... c'mon! What could be more user-friendly than reporting that a user's printer is "on fire"[1]?
(unless, of course, it is just offline)
[1] http://www.kalamazoolinux.org/mailarchive/0006/msg00188.html



