Click here to Skip to main content
15,887,027 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
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 
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 
harold aptroot wrote:
Well I suppose there could be nulls there, but then I'd still test for null rather than just die and later fix it with a catch.


There are all sorts of things that theoretically could go wrong, especially if the object supports inheritance. Checking in advance for all possible exceptions is frequently infeasible at best, and in many situations impossible (since even if the present implementation of a method can't throw an exception, a future override might). If no condition which would cause an exception is expected, such preemptive coding is often a waste of time (both programmer time and execution time).

As for catching an exception and returning an error code versus letting the caller handle it, the latter approach is usually better but not always. If one's objective is to store the output of a function when it works, or -1 if it doesn't, which is nicer:
StoreValue(theFunction())   ' Coded to return -1 in case of failure

or
Dim Result as Double
Try
  Result = theFunction()
Catch
  Result = -1
End Try
StoreValue(Result)

I would consider the former approach to be much nicer.
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 
GeneralRe: how not to check a login Pin
0x3c08-Jul-09 6:46
0x3c08-Jul-09 6:46 
GeneralRe: how not to check a login Pin
icewolf_snowfire8-Jul-09 7:35
icewolf_snowfire8-Jul-09 7:35 
GeneralRe: how not to check a login Pin
Lutosław8-Jul-09 12:39
Lutosław8-Jul-09 12:39 
GeneralRe: how not to check a login Pin
icewolf_snowfire8-Jul-09 15:41
icewolf_snowfire8-Jul-09 15:41 

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.