Re: What do you think about the code?



goose wrote:
Ian Collins wrote:

goose wrote:


Do you program in a language other than C? Do you use exceptions
in those languages? Exceptions are just another form of gotos,
only its "goto that location and tell the code there that this
happened" and not just "goto the error location, it will
unconditionally execute code to handle errors". No one ever seemed
to have trouble refactoring code which generated excetpions.


I do, several. As you say in the next paragraph, exceptions provide a
structured way of passing control. They are /not/ another form of goto,
they do a lot more than simply jumping to another location. There are


Well, the issue going back and forth here is not "spurious usage
of gotos" is it? All the examples posted here were reasoned
and informed uses of gotos to *specifically* handle errors. I'd
hardly call the usage of gotos as we've seen here in this thread
/unstructured/. All were written to simulate exceptions in a language
which doesn't have built in support for exceptions.

In the context they were presented, you could get a way with a goto.
Things beak down when inner loops are extracted into functions.

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.

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.

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

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.

--
Ian Collins.
.



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: 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)
  • Re: What do you think about the code?
    ... Within a fixed scope, but you still identify the error handler (the ... If exceptions are available, I'd rather throw ... I'd chuck in a goto rather than have the logic code do ... longjmp cannot call destructors automatically like ...
    (comp.lang.c)