Re: What do you think about the code?



Ian Collins wrote:
goose wrote:
Ian Collins wrote:


<snipped>

Exceptions are a structured method for reporting errors; can you
disagree with that statement?

I haven't, if you read what I posted, you'll see I agreed.

Just making sure :-)


Using gotos to simulate exceptions would be as close as one
can get to exceptions in C.

Within a fixed scope, but you still identify the error handler (the
label) at the point of error, where throwing an exception does not.

Thats right; However I remain to be convinced that this is such
a bad thing. For a function of, say, ten lines (with the error-handling
code) I'd not hesitate to use a goto *when the error handler is
in the same scope*. If exceptions are available, I'd rather throw
an exception but if exceptions are *not* there for me to use
I'd chuck in a goto rather than have the logic code do
unneeded checks.


I think you countered your own argument when you suggested exceptions
can be implemented with longjump, which is closer than goto.

Closer to what?


plenty of techniques for writing exception safe code. I can't think of
any for writing goto safe code!

If exceptions simply did a longjump, people wouldn't use them.


Fair enough; longjmp cannot call destructors automatically like
C++/Java/etc exceptions can but it *can* unwind the stack.
The jmp_buf initialised with setjmp only gets invalidated when the
function scope where setjmp is called returns[1]. I'd assume that,
with language support for destructors (variables going out of
scope automatically get a certain function called), exceptions
can be implemented with longjmp.

True, but we were talking about goto.


Fair enough; I sorta dragged longjmp into this argument because
I consider it to be fairly close in function to a goto. We can
always drop the longjmp argument if you feel that it's a little
more structured.

Back to gotos: I still think that they have a use; sure they
can make code unstructured, but then again just about any
construct can be abused to make code unstructured including
while, for, do and if.

I've never actually *used* goto in any of my code (well, maybe
once :-) but as a maintainer I see "goto error_exit" all the
time in code that is reasonably easy to read.

OTOH, I've had occasion to completely rewrite some "code"
(and I use that term loosely!) because it was so badly written
that it was faster to rewrite than try to figure out how it
worked and what all the status variables meant; yet the code
contained no gotos and only the usual suspects (while, for, etc).

Theres probably a lesson in there somewhere. Perhaps the
"goto error_exit" users are just more likely to have
structured their functions into "init, solve, exit" than
the coders who avoid goto at all costs.

goose,

.



Relevant Pages

  • Re: What do you think about the code?
    ... Using gotos to simulate exceptions would be as close as one ... Within a fixed scope, but you still identify the error handler (the ... I'd chuck in a goto rather than have the logic code do ...
    (comp.lang.c)
  • Re: What do you think about the code?
    ... Well that's team democracy at work. ... Exceptions are just another form of gotos, ... only its "goto that location and tell the code there that this ...
    (comp.lang.c)
  • Re: What do you think about the code?
    ... Do you program in a language other than C? ... Exceptions are just another form of gotos, ... only its "goto that location and tell the code there that this ... Within a fixed scope, but you still identify the error handler (the ...
    (comp.lang.c)
  • Re: Java needs "goto" (was Re: hi)
    ... Assemblers need "goto" but no decent HLL does. ... I don't use this construction in C either and, again, don't need it. ... Yes - but that's a result of classes that throw exceptions when their methods would be better off returning control values. ... martin@ | Martin Gregorie ...
    (comp.lang.java.programmer)
  • Re: What do you think about the code?
    ... Do you program in a language other than C? ... Exceptions are just another form of gotos, ... only its "goto that location and tell the code there that this ... Anything allocated on the stack will get freed when longjmp gets ...
    (comp.lang.c)