Click here to Skip to main content
15,886,873 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
josda10009-Jul-09 3:31
josda10009-Jul-09 3:31 
GeneralRe: !(How to best use a try catch block) Pin
Dan Neely9-Jul-09 3:43
Dan Neely9-Jul-09 3:43 
GeneralRe: !(How to best use a try catch block) Pin
molesworth9-Jul-09 3:39
molesworth9-Jul-09 3:39 
GeneralRe: !(How to best use a try catch block) Pin
Andrew Rissing9-Jul-09 3:51
Andrew Rissing9-Jul-09 3:51 
GeneralRe: !(How to best use a try catch block) Pin
molesworth9-Jul-09 5:42
molesworth9-Jul-09 5:42 
GeneralRe: !(How to best use a try catch block) Pin
singh.iz.king9-Jul-09 12:58
singh.iz.king9-Jul-09 12:58 
GeneralRe: !(How to best use a try catch block) Pin
0x3c09-Jul-09 5:42
0x3c09-Jul-09 5:42 
GeneralRe: !(How to best use a try catch block) Pin
supercat99-Jul-09 10:40
supercat99-Jul-09 10:40 
If the total is expected to always be computable, but it would be better to return -1 than to throw an exception if it isn't, how would you suggest coding it? Testing all four values to see if they're valid, testing to see whether any of the values is so big as to risk overflow, etc. would seem more complicated than just catching an exception. To be sure, such an approach would yield better performance in the case where an exception would be thrown and caught, but worse performance in the expected case where the total can be computed without difficulty.

With regard to the relative wisdom of returning -1 versus propagating the exception, that would depend upon what the calling program is going to do with the information. It's not hard to imagine situations where having the function return -1 will be much cleaner than having it throw an exception, and others where the reverse would be true.

BTW, one thing I would think would be useful in many situations where some callers would like exceptions handled and others would like them stifled would be for a method to accept a delegate to be called in case of trouble. Such a delegate could throw an exception if necessary, but could also handle some "trouble" situations better than would an exception.
GeneralRe: !(How to best use a try catch block) Pin
harold aptroot9-Jul-09 11:01
harold aptroot9-Jul-09 11:01 
GeneralRe: !(How to best use a try catch block) Pin
supercat910-Jul-09 6:09
supercat910-Jul-09 6:09 
GeneralRe: !(How to best use a try catch block) Pin
harold aptroot10-Jul-09 6:24
harold aptroot10-Jul-09 6:24 
GeneralRe: !(How to best use a try catch block) Pin
supercat910-Jul-09 9:36
supercat910-Jul-09 9:36 
GeneralRe: !(How to best use a try catch block) Pin
Thomas Weller10-Jul-09 0:29
Thomas Weller10-Jul-09 0:29 
GeneralRe: !(How to best use a try catch block) Pin
singh.iz.king10-Jul-09 3:43
singh.iz.king10-Jul-09 3:43 
GeneralRe: !(How to best use a try catch block) Pin
supercat910-Jul-09 6:02
supercat910-Jul-09 6:02 
GeneralRe: !(How to best use a try catch block) Pin
Thomas Weller10-Jul-09 18:46
Thomas Weller10-Jul-09 18:46 
GeneralRe: !(How to best use a try catch block) Pin
supercat913-Jul-09 5:19
supercat913-Jul-09 5:19 
GeneralRe: !(How to best use a try catch block) Pin
OriginalGriff13-Jul-09 8:52
mveOriginalGriff13-Jul-09 8:52 
GeneralRe: !(How to best use a try catch block) Pin
supercat914-Jul-09 6:10
supercat914-Jul-09 6:10 
GeneralRe: !(How to best use a try catch block) Pin
BadKarma10-Jul-09 1:25
BadKarma10-Jul-09 1:25 
GeneralRe: !(How to best use a try catch block) Pin
singh.iz.king10-Jul-09 3:50
singh.iz.king10-Jul-09 3:50 
GeneralRe: !(How to best use a try catch block) Pin
Member 448708313-Jul-09 8:17
Member 448708313-Jul-09 8:17 
GeneralRe: !(How to best use a try catch block) Pin
singh.iz.king13-Jul-09 15:20
singh.iz.king13-Jul-09 15:20 
GeneralRe: !(How to best use a try catch block) Pin
mateotrek18-Aug-09 19:19
mateotrek18-Aug-09 19:19 
Generalhow not to check a login [modified] PinPopular
icewolf_snowfire8-Jul-09 6:11
icewolf_snowfire8-Jul-09 6:11 

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.