Click here to Skip to main content
15,892,005 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: See the writing on the wall... Pin
BillW334-May-12 2:45
professionalBillW334-May-12 2:45 
GeneralRe: See the writing on the wall... Pin
Manfred Rudolf Bihy3-May-12 2:56
professionalManfred Rudolf Bihy3-May-12 2:56 
GeneralRe: See the writing on the wall... Pin
CDP18023-May-12 4:03
CDP18023-May-12 4:03 
GeneralRe: See the writing on the wall... Pin
OriginalGriff3-May-12 6:01
mveOriginalGriff3-May-12 6:01 
GeneralRe: See the writing on the wall... Pin
CDP18023-May-12 12:36
CDP18023-May-12 12:36 
GeneralRe: See the writing on the wall... Pin
Brisingr Aerowing3-May-12 6:36
professionalBrisingr Aerowing3-May-12 6:36 
GeneralRe: See the writing on the wall... Pin
spotsknight3-May-12 8:43
spotsknight3-May-12 8:43 
GeneralRe: See the writing on the wall... Pin
CDP18023-May-12 12:26
CDP18023-May-12 12:26 
What you are doing there is controlling the program flow by catching the exception and doing nothing else with it. That's not very good because exception handling is very slow. A simple 'if' to check wether you have gotten any rows would suffice and your code would be faster and besides that easier to read and understand. ExecuteScalar() is a bit ugly because it returns an object if I remember right, so you must check for a null reference and after that use an adequate type cast.

Besides that, just take exceptions by their name. They point out unexpected or unusual events. Don't use them for anything you expect. I usually take care of three cases:

Some exceptions are avoidable. They are just cases which I did not consider in my code. This would be the NullReferenceException for example. Checking for null at the proper places is easy enough, but once in a while you simply forget it or did not expect a null value at that location. To prevent the program from terminating there should be a 'line of defense' where all exceptions are caught and at least are logged in some way, together with a stack trace and, if possible, any other relevant parameters. If you go through the trouble to read the log and then eliminating the causes of the exceptions, then you have a mechanism that's worth more than many unit tests and you will eventually get a very robust program.

Then there are correctable exceptions. Obviously it's then a good idea to do whatever needs to be done to correct the problem in the catch block. Again, avoid the exception if you can. This is only for those cases you can't handle in any other way. And log it. When using your last option shows up too often in the log, it's time to consider improving the code.

And then there are exceptions you cannot foresee, like calling a web service and getting a timeout because the server does not respond. Nothing much you can do about that except trying again later (or taking a look at the server if you can). Anyway, whatever you wanted to do is impossible for now and the exception then should be caught so that the following now futile steps are omitted.
At least artificial intelligence already is superior to natural stupidity



GeneralRe: See the writing on the wall... Pin
spotsknight4-May-12 4:37
spotsknight4-May-12 4:37 
GeneralRe: See the writing on the wall... Pin
Jörgen Andersson4-May-12 10:31
professionalJörgen Andersson4-May-12 10:31 
GeneralRe: See the writing on the wall... Pin
spotsknight4-May-12 12:31
spotsknight4-May-12 12:31 
GeneralRe: See the writing on the wall... Pin
Chris Ross 28-May-12 5:51
Chris Ross 28-May-12 5:51 
JokeRe: See the writing on the wall... Pin
Tom Delany3-May-12 9:51
Tom Delany3-May-12 9:51 
GeneralRe: See the writing on the wall... Pin
CDP18023-May-12 12:33
CDP18023-May-12 12:33 
GeneralRe: See the writing on the wall... Pin
RobEpworth3-May-12 18:47
RobEpworth3-May-12 18:47 
GeneralRe: See the writing on the wall... Pin
Matthys Terblanche3-May-12 21:44
Matthys Terblanche3-May-12 21:44 
GeneralRe: See the writing on the wall... Pin
Jeremy Hutchinson4-May-12 1:32
professionalJeremy Hutchinson4-May-12 1:32 
GeneralRe: See the writing on the wall... Pin
CDP18024-May-12 2:24
CDP18024-May-12 2:24 
GeneralRe: See the writing on the wall... Pin
Jeremy Hutchinson4-May-12 2:29
professionalJeremy Hutchinson4-May-12 2:29 
GeneralRe: See the writing on the wall... Pin
Dan Neely4-May-12 2:37
Dan Neely4-May-12 2:37 
GeneralRe: See the writing on the wall... Pin
CDP18024-May-12 2:41
CDP18024-May-12 2:41 
GeneralRe: See the writing on the wall... Pin
JackDingler4-May-12 4:27
JackDingler4-May-12 4:27 
GeneralRe: See the writing on the wall... Pin
bkebamc4-May-12 5:47
bkebamc4-May-12 5:47 
GeneralRe: See the writing on the wall... Pin
KP Lee5-May-12 9:50
KP Lee5-May-12 9:50 
Generalshame to my tester or me? PinPopular
kdgupta871-May-12 21:15
kdgupta871-May-12 21:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.