April 01, 2004
On Thu, 01 Apr 2004 16:12:43 +1000, "Derek Parnell" <Derek.Parnell@psyc.ward> wrote:
> On Thu, 01 Apr 2004 06:07:58 GMT (01/Apr/04 04:07:58 PM)
> , Karl Bochert <kbochert@copper.net> wrote:
> 
> > On Wed, 31 Mar 2004 21:48:20 -0800, Andy Friesen <andy@ikagames.com> wrote:
> >> Karl Bochert wrote:
> >> >>
> >> >>   try { <some statement(s) }
> >> >>   catch (<errorclass>) { <do something about it> }
> >> >>   finally { <always run> };
> >> >>
> >> >
> >> > How does that differ from:
> >> >
> >> >     try { <some statement(s) }
> >> >     catch (<errorclass>) { <do something about it> }
> >> >     <always run>};
> >>
> >> Even if an exception is thrown, and isn't caught in this scope, the finally block will execute while the stack is being unwound.
> >>
> >> ie
> >>
> >> try {
> >>     throw new Exception("This won't be caught here.");
> >> } catch (IOError error) {
> >>     we can't catch Exception() here, only IOError
> >> } finally {
> >>     // clean up the file, whether or not an error occurred
> >>     myFile.close();
> >> }
> >>
> >>   -- andy
> > Makes sense   -- sort of interleaved exceptions
> > Not for me, I think
> 
> How come, Karl?
> 
> I can see that I might need to close a file regardless of what type of error happened, and that I might also want to do some special stuff for specific types of errors.
> 
> That's what the 'finally' phrase allows for.
> 

Actually, instead of

    if (error)  throw <myerr>   ;
    ...
    finally {
        <cleanup>
        }

I would prefer:

    if (error) {
        <cleanup>;
        throw <myerr>
        }

Related information is close in the code.
More understandable, especially if the <cleanup> is parameterized.
I prefer to avoid complex flow of control, especially in error handling which is difficult to exhaustively test.

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>
     }
etc.
Says what it does, where it does it.


I shudder to think of what errors a not-quite D expert could introduce.
I shudder to think of what correct code an very expert D programmer could write.

I think I'm too old for this business :-)
Karl Bochert

P.S.
Come to think of it, what does 'try' accomplish?