[lug] g++ signals and exceptions question

Tkil tkil at scrye.com
Wed May 15 14:05:00 MDT 2002

>>>>> "jb" == Jonathan Briggs <zlynx at acm.org> writes:

jb> Just like with other exceptions, I would have exception handlers
jb> deeper in the code if there was a section that needed to exit with
jb> a known state.

That "exit with a known state" is really nothing more than "finish a
sub-stage of processing and go on with the next", is it not?  This is
almost certainly more expensive than checking a global variable.  :)

And this is where we get into a religious debate about exactly what we
should use exceptions for.  :)  

There are languages that don't offer any exceptions (e.g., C); there
are those that use them for everything (Java in some ways, Perl6 will
be this way).

My opinion is somewhere in between, but more on the side of very few
exceptions.  It seems that exceptions are best reserved for things
that truly *are* exceptional.  If it's something that you expect to
happen most of the time in the normal process of your program, than I
should check for it explicitly.

Some examples to think about:

1. End of file.  Some systems use an exception to signal this.  Others
   use a "can't really exist" value to signal EOF (libc, perl).

2. Name lookup failure.

3. Out of memory.

Circumstances can change how I feel about exceptions.  For library
designers, they do allow a better abstraction for reporting situations
that are outside their ability to deal with.  They can actually
increase clarity by separating normal data flow from "out of band"
data.  I don't know if I like exceptions if they're used just as an
idiomatic shortcut.

It might be worthwhile to compare real-world examples at this point,
instead of just listening to me babble on.  :)  If I get more time
later today, I will try to write a few different versions of reading a
file to EOF, and have people tear them apart and/or reconstruct
idiomatically in other languages.

Anyway, glad you have a solution for now.


More information about the LUG mailing list