Thoughts from Dan Miser RSS 2.0
 Tuesday, January 11, 2005
It's been a while since I blogged a "pet peeve", and I know you're all anxiously awaiting the next entry in the series. :-)

When you see code like this, run like mad!


try
  DoSomething;
except
end;

Note: You can have the same bad code in C#, and other languages, too.

The problem with this code is that you are swallowing ALL exceptions in this code block. For example, you now won't be notified of Access Violations, Out of Memory conditions, Invalid Pointer operations, or other system-level problems. If DoSomething is throwing an exception of a known type, handle it in the except block explicitly. If you don't know what exceptions can come back, don't handle them. It's really that simple. You can't count on your program working correctly from this point forward if you use this type of construct.

Now, I can think of some very rare places where you need this type of logic (e.g. if an exception leaks out, it will crash a poorly written host process, like IIS and ISAPI). But even then, it would be best to at least log the error somewhere (e.g. file, EventLog, whatever).

I'm almost done writing a very exhaustive paper on exception handling that I titled "Hitch Hiker's Guide To Exceptions". With any luck, I'll publish it to BDN. Stay tuned, and I'll certainly blog about that when/if it gets submitted and accepted.

Tuesday, January 11, 2005 10:42:00 AM (Central Standard Time, UTC-06:00)  #    Comments [7] -

Tracked by:
http://9nh-information.info/09801490/index.html [Pingback]
http://9nw-information.info/55721990/faithstart-christian-magazine-index.html [Pingback]
http://9no-information.info/16154486/wifi-laptop-cards.html [Pingback]
http://9nn-information.info/84541831/online-software-interior-design.html [Pingback]
http://9nv-information.info/41397648/soft-stick-audio-hack.html [Pingback]
http://9na-information.info/74134739/index.html [Pingback]
http://9nw-information.info/46792669/index.html [Pingback]
http://9ny-information.info/19777415/music-on-my-psp.html [Pingback]
http://9na-information.info/17261742/waste-water-characteristic-study-report.htm... [Pingback]
http://9on-information.info/15924746/index.html [Pingback]
http://9oq-information.info/46286416/index.html [Pingback]
http://9om-information.info/94129880/index.html [Pingback]
http://9oy-information.info/97464372/wild-orchid-hotel-angeles-city-philippines.... [Pingback]
http://9qp-information.info/63398800/index.html [Pingback]
http://9om-information.info/56496465/sturgeon-fish-in-suwanee-river-florida.html [Pingback]
http://9of-information.info/71061306/fleetwood-mac-the-ones-song-lyrics.html [Pingback]
http://9qq-information.info/44217371/index.html [Pingback]
http://9rt-information.info/30449125/define-mentoring-program-at-the-work-place.... [Pingback]
http://9st-information.info/32608266/spartito-musicali-religiosi.html [Pingback]
http://9sd-information.info/68433992/fidanzata-kate-moss.html [Pingback]
http://9uaed-le-informazioni.info/04710155/scaricare-sito-completo.html [Pingback]
http://9uaeg-le-informazioni.info/27077327/index.html [Pingback]
http://9uaes-le-informazioni.info/80361445/vetro-portiera-sx-x1-9.html [Pingback]
http://9uaeq-le-informazioni.info/27457599/screensaver-cavallo.html [Pingback]
http://9uaeb-le-informazioni.info/63772938/index.html [Pingback]
http://9uafj-le-informazioni.info/43890896/index.html [Pingback]
http://9uafo-le-informazioni.info/91912660/aumenta-comfort-termico-ed-acustico-c... [Pingback]
http://9uaei-le-informazioni.info/02895844/index.html [Pingback]
http://9uaen-le-informazioni.info/81581770/index.html [Pingback]
http://9uahl-le-informazioni.info/47623120/fondo-di-assistenza-sanitaria-turismo... [Pingback]
http://9uagt-le-informazioni.info/19324817/index.html [Pingback]
http://9uagc-le-informazioni.info/28397832/index.html [Pingback]
http://9uagm-le-informazioni.info/46232236/index.html [Pingback]
http://9uahn-le-informazioni.info/39153990/acquerello-mostra.html [Pingback]
http://9uagm-le-informazioni.info/53062258/index.html [Pingback]
http://9uaho-le-informazioni.info/81458261/index.html [Pingback]
http://9uagk-le-informazioni.info/03784860/aereo-napoli-torino.html [Pingback]
http://9uaha-le-informazioni.info/28056508/thinkpad-380ed.html [Pingback]
http://9uagt-le-informazioni.info/40639199/index.html [Pingback]
Tuesday, January 11, 2005 11:44:00 AM (Central Standard Time, UTC-06:00)
I thought Nick Hodges said this was a valid piece of code?
Tuesday, January 11, 2005 2:17:00 PM (Central Standard Time, UTC-06:00)
I agree with you on this one. It is also a huge pet peve of mine. It is even worse with C# and Visual Studio since when you are debugging in the IDE it doesn't tell you about unhandled exceptions. I inherited a large pile of C# code full of these. What a nightmare. Finally decided to rewrite it.
Tuesday, January 11, 2005 3:53:00 PM (Central Standard Time, UTC-06:00)
I'm with you 100% on this one, Dan.



Doing that is like lobotomising the exception-system.



Unfortunately, I know that some companies actually encourage this kind of coding. They feel it is better to hide bugs and have "small failures" in the application than to bother the user with exception messages.



I think some programmers get this bad habit from their Java experience where the insanely awkward exception-specification system often triggers this kind of dirty "solution".

Wednesday, January 12, 2005 1:26:00 AM (Central Standard Time, UTC-06:00)
Chris, that is a valid piece of code. That doesn't mean you should code that way.



Dan, please tell me your paper will have details on exception handling in threads. Threads and exceptions are the single biggest bain of my existance.
Robert MacLean
Wednesday, January 12, 2005 9:35:00 AM (Central Standard Time, UTC-06:00)
Great point, Robert. I actually didn't have that in my paper, but I will add it now. Thanks.
Monday, January 31, 2005 9:22:00 AM (Central Standard Time, UTC-06:00)
We do use this construction to prevent errors that are not at all "business-related" from disturbing or stopping the main application.



In the example below, we never ever want to see an exception coming out of the logging function. It would be insane to halt the application's execution just because writing to an unimportant text log file fails.



procedure LogSomething(const Msg: string);

try

WriteToTheTextLog(Msg);

except

// errors while writing to the log

// should never stop the real program from

// continuing

end;
Robert Houdart
Tuesday, November 07, 2006 9:19:00 AM (Central Standard Time, UTC-06:00)
Read the overview doc and impressed at what DLinq has to offer. Esp. pleased to see transactions and stored procedures as part of the design. This would be a welcome addition to the .net framework and ADO.

Comments are closed.
Navigation
Archive
<August 2008>
SunMonTueWedThuFriSat
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2008
Dan Miser
Sign In
Statistics
Total Posts: 306
This Year: 21
This Month: 0
This Week: 0
Comments: 604
All Content © 2008, Dan Miser
DasBlog theme 'Business' created by Christoph De Baene (delarou)