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;
try
try
// code that may raise an exception
except
// log exception
raise;
end;
except // yes, this except block is empty
end;
A Pokemon exception - catch them all.
ReplyDeleteOliver Funcke http://blog.codinghorror.com/new-programming-jargon/
ReplyDeleteOliver Funcke Yes, but re-raise them first!
ReplyDeleteThat one is harmless. I see 5k LOC constructors all day.
ReplyDeleteOliver Funcke http://misko.hevery.com/code-reviewers-guide/flaw-constructor-does-real-work/
ReplyDeleteI know that. But it gets worse: The solution implemented to hide the monster constructors is actually Baklava code.
ReplyDeleteApparently a "Jimmy Coder" ...
ReplyDeleteWow. 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]
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.
ReplyDeleteThanks 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