High performance code of the day

High performance code of the day

try
  try
    // code that may raise an exception
  except
    // log exception
    raise;
  end;
except // yes, this except block is empty
end;

Comments

  1. A Pokemon exception - catch them all.

    ReplyDelete
  2. Oliver Funcke Yes, but re-raise them first!

    ReplyDelete
  3. That one is harmless. I see 5k LOC constructors all day.

    ReplyDelete
  4. I know that. But it gets worse: The solution implemented to hide the monster constructors is actually Baklava code.

    ReplyDelete
  5. Apparently a "Jimmy Coder" ...
    Wow. Yeah, I'll admit it, until last year; I didn't know an exception continues to bubble out of a "try..finally..end"... had to go through an audit code and stick an "except" into the "try". I know, I suck. I know, I must be dumb. But yeah, many, many (indie) years of Delphi and I didn't know.

    Q: This is my new code-block, after sticking a "except" into it's existing "finally" structure. Is this Best Practice now for handling?

    [code]
    // bSuccess := FALSE; * removed this line after added-for-except, as the compiler bitches
    MyObj := TMyObj.create();
    try
       try // * added-for-except
          MyObj.DoSomething();
          bSuccess := TRUE:
       except // * added-for-except
          bSuccess := FALSE;
       end; // * added-for-except
    finally
       MyObj.Free();
    end;
    if (bSuccess) then DoAFunction();
    [/code]

    ReplyDelete
  6. Vin Colgin If it does not hurt if MyObj is allocated for the time of the DoAFunction execution then just call DoAFunction in the line where you currently set bSuccess to True.

    ReplyDelete
  7. Thanks for the confirmation Stefan Glienke of the layout. Admittedly, I tend to write very conventional procedural code with error-codes; hence the desire to use "bSuccess" and get out of the "try..end" block asap. Went through some code from 1998, I used to used "except" everywhere and "finally" was nowhere to be found! I guess when I switched to "finally", I thought it did both. Wrong! Funny to still have that old stuff. I used to work for Hollywood Video in the 90s, they were big into Delphi and StarTeam.

    ReplyDelete

Post a Comment