Andrew Lentvorski wrote:
However, generally the people who whinge about non-local control wind up creating deep conditionals checking bunches of error codes. And what do they put all over that code:

    // Do the initial thing

    if (foocondition) {
    // Do cleanup
    return FALSE;
    }

    if (barcondition) {
    // Do cleanup
    return FALSE;
    }

    <finally do the real work>

Which are all effectively non-local control gotos.  Just like exceptions.
Oh LORD no! I'll wrap a C++ programmer's knuckles for that. That's the definitive case for NOT doing RAII.

Now, with Java, because of exactly this other problem I mentioned of the lexically based release of resources not being generalized beyond its own built in monitors, does have to do this in a more ugly fashion, but the idiom is still based on local control and generally avoids checking error codes:

SomeClass foo;
try {
   foo = ...;
   SomeOtherClass bar;
   try {
       bar = ....;
       ....
   } finally {
       if (null != bar) {
           bar.finalize();
        }
   }
} finally {
   if (null != foo) {
       foo.finalize();
   }
}

In some cases the if checks are unnecessary. In others you use something more specific than finalize(), and that helps avoid having to worry about certain exceptions having been thrown. It sure is a lot uglier than the C++ equivalent, and it sure doesn't provide nearly as nice encapsulation of resource management. Either way, the only resources you should have to worry about cleaning up are the ones you acquire in the same context (i.e. it local resources). C# is mildly better with it's "using" keyword, which is essentially a more generalized version of Java does with synchronization primitives.

--Chris

--Chris

--
[email protected]
http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-lpsg

Reply via email to