Return in finally

Frank Benoit (keinfarbton) benoit at tionex.removethispart.de
Wed Mar 7 23:54:29 PST 2007


> Does this work?

yes, this would handle the return, but it would not allow a second
try/catch in the finally block.

What i use now is this:
// D ==========================
int func(){
  ...
  {
    bool   tioport_stat_set_1;
    int    tioport_stat_val_1;
    Object tioport_stat_exc_1;
    try{
      try{
        // (1)
      tryend_1:;
      } catch( IOException e ){
        // (2)
      }
      catchend_1:;
    } catch( Object e ) {
      tioport_stat_exc_1 = e;
    }
    {
      // (3)
    }
    if(tioport_stat_exc_1 !is null) throw tioport_stat_exc_1;
    if(tioport_stat_set_1) return tioport_stat_val_1;
  }
}

In (1) und (2) replace all "return expr;" with
{ tioport_stat_set_1 = true;
  tioport_stat_val_1 = expr; // if has a return val
  goto tryend_1;             // or catchend_1
}

Now the finally block is a normal code block.
It is possible to have more try/catch in the finally block.
It is possible to return/throw from the finally block



More information about the Digitalmars-d mailing list