April 01, 2004
On Thu, 01 Apr 2004 13:36:08 -0600, Brad Anderson <brad@dsource.dot.org> wrote:
> 
> 
> Karl Bochert wrote:
> 
>  > If complex error handling is needed, I would refactor or do something
> like:
>  >     try {
>  >         if (err) {
>  >             <pre_cleanup>
>  >             throw <myerr>
>  >             }
>  >     }
>  >     catchall {
>  >         if (curerr == ioerr)  <cleanup_io>
>  >         else if (err == myerr ) <cleanup>
>  >         else throw <curerr>
>  >      }
> 
> 
> in your example above, for 'try { }', the 'if (err) { }' portion is built in.  That's what try { } does, it automatically has a 'if (err) { }'.  The code to execute in the 'if (err) { }' is contained in the 'catch (err) { }' block.

Then I don't understand at all!  Some piece of code thows an error, and it only does so when there is an error
to thow.  Therefore the 'if(err) throw' -- you don't want to throw if there is no error.
The above case is a little unusual (i think) in that the same function is both throwing and catching its own error.
(Thats all one function)
The throw statement walks back the stack looking for a catch to handle it.
If the 'finally' is meant to cleanup before the throw starts its walk, then the cleanup can be inserted before the
throw as above.  Alternatively the thow could begin its walk at the current subroutine, which could catch its
own throw and cleanup there. either way, the finally clause is superfluous.

Why should there be a try clause?

    foo()
   {
       <code1>
       <code2>
       if (somethings_wrong)  throw;
       <code3>
       catch {
            if (myerr)  <cleanup and re-throw>
        }
    }

What does putting <code2> and the throw in a try clause accomplish?


> 
>  > Come to think of it, what does 'try' accomplish?
> 
> 
> I have found that once I got used to the try-catch-finally, it's like cocaine (You can't do without it).  It helps you get closer to bullet-proof code.
> 
Perl programmers say the same thing about all manner of obfuscatory features :-)


Uncomprehending
Karl Bochert