Click here to Skip to main content
15,849,829 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: !(How to best use a try catch block) Pin
supercat99-Jul-09 11:40
supercat99-Jul-09 11:40 
GeneralRe: !(How to best use a try catch block) Pin
harold aptroot9-Jul-09 12:01
harold aptroot9-Jul-09 12:01 
GeneralRe: !(How to best use a try catch block) Pin
supercat910-Jul-09 7:09
supercat910-Jul-09 7:09 
GeneralRe: !(How to best use a try catch block) Pin
harold aptroot10-Jul-09 7:24
harold aptroot10-Jul-09 7:24 
GeneralRe: !(How to best use a try catch block) Pin
supercat910-Jul-09 10:36
supercat910-Jul-09 10:36 
GeneralRe: !(How to best use a try catch block) Pin
Thomas Weller10-Jul-09 1:29
Thomas Weller10-Jul-09 1:29 
GeneralRe: !(How to best use a try catch block) Pin
singh.iz.king10-Jul-09 4:43
singh.iz.king10-Jul-09 4:43 
GeneralRe: !(How to best use a try catch block) Pin
supercat910-Jul-09 7:02
supercat910-Jul-09 7:02 
Thomas Weller wrote:
NEVER ever throw an exception to code a foreseeable condition - even if it might be rare.


I guess it depends what you mean by 'foreseeable'. Certainly if one could readily determine before performing an operation that it would throw an exception that one was planning on catching, and if there was any significant likelihood of such exception occurring, doing an early test before attempting the operation would be a good idea. Testing the length test on the array, for example, would seem appropriate, though in some applications that may be just about the only exception that could be guaranteed not to occur.

On the other hand, it may not be possible to determine in advance all exceptions that might occur. In the code sample given, the '.Value' property could throw an exception (e.g. ObjectDisposed or whatever) even if the length of the array was valid. While a general 'Catch' would be icky, there are times when such a thing is appropriate. For example, in expression-evaluation window, nearly all exceptions thrown during expression evaluation should be caught. An application shouldn't die just because someone typed in an expression that caused an exception the application programmer hadn't foreseen.

I don't particularly like -1 as a return value, but the overall style would seem appropriate for some applications.
GeneralRe: !(How to best use a try catch block) Pin
Thomas Weller10-Jul-09 19:46
Thomas Weller10-Jul-09 19:46 
GeneralRe: !(How to best use a try catch block) Pin
supercat913-Jul-09 6:19
supercat913-Jul-09 6:19 
GeneralRe: !(How to best use a try catch block) Pin
OriginalGriff13-Jul-09 9:52
mveOriginalGriff13-Jul-09 9:52 
GeneralRe: !(How to best use a try catch block) Pin
supercat914-Jul-09 7:10
supercat914-Jul-09 7:10 
GeneralRe: !(How to best use a try catch block) Pin
BadKarma10-Jul-09 2:25
BadKarma10-Jul-09 2:25 
GeneralRe: !(How to best use a try catch block) Pin
singh.iz.king10-Jul-09 4:50
singh.iz.king10-Jul-09 4:50 
GeneralRe: !(How to best use a try catch block) Pin
Member 448708313-Jul-09 9:17
Member 448708313-Jul-09 9:17 
GeneralRe: !(How to best use a try catch block) Pin
singh.iz.king13-Jul-09 16:20
singh.iz.king13-Jul-09 16:20 
GeneralRe: !(How to best use a try catch block) Pin
mateotrek18-Aug-09 20:19
mateotrek18-Aug-09 20:19 
Generalhow not to check a login [modified] PinPopular
icewolf_snowfire8-Jul-09 7:11
icewolf_snowfire8-Jul-09 7:11 
GeneralRe: how not to check a login Pin
0x3c08-Jul-09 7:46
0x3c08-Jul-09 7:46 
GeneralRe: how not to check a login Pin
icewolf_snowfire8-Jul-09 8:35
icewolf_snowfire8-Jul-09 8:35 
GeneralRe: how not to check a login Pin
Lutosław8-Jul-09 13:39
Lutosław8-Jul-09 13:39 
GeneralRe: how not to check a login Pin
icewolf_snowfire8-Jul-09 16:41
icewolf_snowfire8-Jul-09 16:41 
GeneralRe: how not to check a login Pin
Lutosław9-Jul-09 5:04
Lutosław9-Jul-09 5:04 
GeneralRe: how not to check a login Pin
leppie12-Jul-09 3:15
leppie12-Jul-09 3:15 
GeneralRe: how not to check a login Pin
icewolf_snowfire14-Jul-09 10:32
icewolf_snowfire14-Jul-09 10:32 

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.