From: D Yuniskis on
Hi Bob,

Bob wrote:
> Back before C++ exceptions, I wrapped setjump/longjump in a struct w/
> heap cleanup func pointers to handler errors that just wouldn't
> reasonably work any other way (floating point and floppy disk errors). I
> treat C++ exceptions just as warily. If the problem can be handled with
> error returns, then you don't need exceptions/longjump.

I've only used setjmp/longjmp to implement simulators and
coroutines. Other than that, I think your "error returns"
comment applies best! (i.e., make the problem *visible*)
From: Dennis on
Tim Wescott wrote:

> But used carefully and sparingly, "goto" in C can provide a very good
> mechanism for exception handling. I use it occasionally, and it does
> well for what it is. Were I to see a "goto" in a code review I would
> pin the author of the code against the wall to make sure he was using it
> correctly; were I to enter some of my code with a "goto" in a code
> review I would schedule extra time to be pinned to the wall and justify
> myself (I'd probably have careful comments in the code, too).
>
In our code reviews a goto had to be either a usage that the team had
agreed on (very few - usually related to exception cases) or you had to
defend the usage.

> Exception handling in object-oriented programming is specifically
> designed to address the problems in C++ that C's "goto" and long branch
> (long goto? I can't remember what it's called) solve in C.
>
When we went to C++ most of the error case gotos went away with well
designed destructors (to release resources) and throw() of an exception.
From: Dennis on
David Brown wrote:
> There are times when "goto" is the best way to structure your code -
> I've used it a few times. You have to be careful with it, as it's easy
> to write spaghetti code and lose track of things like variable
> initialisations (C++ is fussier about when you are allowed to use it).
> But setjmp() and longjmp() are far worse - if you think you need them,
> you are almost certainly wrong and should re-structure your code. (If
> you /know/ you need them, it's a different matter - few rules in C
> programming are absolute.)

One of the incentives to move to C++ was the nested setjmp() and
longjmp() in the existing C code (yes setjmp() and longjmp() was the
cleanest solution in C). With matching constructors and destructors in
C++ it was much easier to manage resources automatically on a throw() in
the exception case. Of course C++ takes some careful design up front.
From: Tim Wescott on
On 06/02/2010 10:01 AM, D Yuniskis wrote:
> Hi Tim,
>
> Tim Wescott wrote:
>> Exception handling in object-oriented programming is specifically
>> designed to address the problems in C++ that C's "goto" and long
>> branch (long goto? I can't remember what it's called) solve in C.
>
> setjmp()/longjmp()?

That's it. The more you start doing "C++ish" things the more dangerous
it gets -- but it's kewl for (very, very carefully constructed) library
code-like things.

--
Tim Wescott
Control system and signal processing consulting
www.wescottdesign.com
From: Tim Wescott on
On 06/02/2010 02:24 PM, Dennis wrote:
> Tim Wescott wrote:
>
>> But used carefully and sparingly, "goto" in C can provide a very good
>> mechanism for exception handling. I use it occasionally, and it does
>> well for what it is. Were I to see a "goto" in a code review I would
>> pin the author of the code against the wall to make sure he was using
>> it correctly; were I to enter some of my code with a "goto" in a code
>> review I would schedule extra time to be pinned to the wall and
>> justify myself (I'd probably have careful comments in the code, too).
>>
> In our code reviews a goto had to be either a usage that the team had
> agreed on (very few - usually related to exception cases) or you had to
> defend the usage.

Yup. And if you're smart you take your code to one or two of the folks
you know are going to _really_ care and you run it by them _first_.

>> Exception handling in object-oriented programming is specifically
>> designed to address the problems in C++ that C's "goto" and long
>> branch (long goto? I can't remember what it's called) solve in C.
>>
> When we went to C++ most of the error case gotos went away with well
> designed destructors (to release resources) and throw() of an exception.

And yup.

--
Tim Wescott
Control system and signal processing consulting
www.wescottdesign.com
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5 6 7 8
Prev: ready to run 32bit controller
Next: ARM7