What you're saying is that you want your programs to blow up when a function returns a known error (such as "disk full").
One of the points of the article is that C makes you think about all the possible, well-documented, errors that each function can encounter. You know when you write C precisely what can go wrong at each stage, and decide the right way to handle it.
Seeing a dialog box pop up with some exception backtrace is one of the things that annoys me about 'modern' Java-esque languages: they seem to encourage developers to default to ignoring most exceptions.
I think the space between to two is more uncomfortable than either extreme.
One of the most interesting papers I read was "Why computers fail and what we can do about it" by Jim Gray while he was at Tandem. Transactions are essential, and most failures are transient. This can be applied at many levels, and can deal with many failures without the developer of the mainline code having to decide what to do for each given failure.
if ((result = foo()) == -1) {
fprintf(stderr, "!$*!$&^!$*!!!!\n");
exit(1);
}http://www.on-time.com/ddj0011.htm
I'm almost tempted to make an analogy with scheme's (call-with-current-continuation) here, but I think that might be pushing it.
Failing loudly is the only sensible behavior when an unexpected error occurs and there's absolutely no indication of what to do in such a case.
A great big codesplosion spraying you with a several hundred lines of stack trace and a "Your program has encountered an error an needs to close" is not "sensible behaviour".
Yes it is, if it's a program in development where I haven't yet decided what behavior an error condition should produce.
You guys don't seem to understand I don't necessarily want to decide how to handle all errors now, not that I never want to have to think about how to handle them.
tyger(~)% cat << EOF > main.c
? #include <assert.h>
?
? void main( void )
? {
? assert( 0 == 1 ) ;
? }
? EOF
tyger(~)% gcc main.c
main.c: In function ‘main’:
main.c:4: warning: return type of ‘main’ is not ‘int’
tyger(~)% ./a.out
Assertion failed: (0 == 1), function main, file main.c, line 5.
Abort
tyger(~)%
This is the fault of the programmer who wrote the function, not of the language.
(Example code is OCaml)
type 'a checked = Pass of 'a | Fail
(* very much a toy example *)
let even x = (x mod 2 == 0)
let can_fail x = match x with
| 0 -> Fail
| x when (even x) -> Pass (x * 10)
| x -> Pass (x)
# can_fail 2;;
- : int checked = Pass 20
# can_fail 3;;
- : int checked = Pass 3
# can_fail 0;;
- : int checked = Fail
#
The languages infers where you're passing around option types (such as "int checked" above) and forces the calling function to account for any potential failures. Rather than just Pass 'a and Fail, it could be Pass, File_Not_Found, Access_Denied, etc., and (Unknown_Error string). OCaml also has exceptions, but the combination of union types and required exhaustive matching covers most cases.By the same token, dozens of Java libraries only throw ABCException, with no other information... Might as well be -1.
Exceptions are a language semantic - in itself it doesn't make your code robust. You still need to apply it correctly.
But! The reality is the same applies to C too. What the original author was alluding to what that the mentality of C encourages coders to look deeper into their errors.
Whereas when you're dealing with return codes, the default is to ignore.
It seems like a good compromise.
You mean like when a function silently returns -1 to indicate failure and then you wonder why your program returned a wrong result (if you're lucky enough to even notice)? In the bigger part of most of my programs I want a big, flashy, loud, total failure by default if anything goes wrong.