Re: What do you think about the code?



goose wrote:
Ian Collins wrote:

goose wrote:

Ian Collins wrote:

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.

Well I certainly couldn't justify a goto in a ten line function, then
again I seldom write functions much longer than 10 lines :)

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


Closer to what?

Exceptions.

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.

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.

It's all a matter of taste and style. As my teams and I use Test Driven
Development, code is refactored often, so any construct that causes
problems with this isn't appropriate.

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.

Ah, the truth emerges!

--
Ian Collins.
.



Relevant Pages

  • Re: Error handling in VB6
    ... The VB syntax for catching and handling errors is definitely not ... exceptions thrown in Main, and in anything called from Main. ... OnError GoTo SomeErrHandler ... VB6 has no such thing as a global error handler. ...
    (microsoft.public.vb.general.discussion)
  • 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)