HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Exception Handling: First or Last?

12-24-2013, 09:15 PM#1
Ignitedstar
I have a question for all coders. When you do exception handling for what you code, do you put your exception handling before the code that performs what the function actually does, or do you put them afterwards?

I *assume* (and I could be wrong) in some cases it would matter where you put the exception handling, but where they go would account for how each person codes, wouldn't it?. Is there a proper way to do it? Or at least, what's a good way to make code more readable?

Where do you put them when you nest functions? Does it change? Should it matter?

Example: (whether this function actually works isn't the point, the point is where the exception handling occurs)

Collapse JASS:
function AddTwoNumbers takes int x, int y returns nothing
    local integer total = 0
    //When exception handling comes before the meat of the function
    if x <= 0 or y <= 0 then
        BJDebugMsg("Please use numbers greater than zero.")
    else
        set total = x + y
        BJDebugMsg(I2S(total))
    endif
endfunction

or:

Collapse JASS:
function AddTwoNumbers takes int x, int y returns nothing
    local integer total = 0
    //When exception handling comes after the meat of the function
    if x > 0 and y > 0 then
        set total = x + y
        BJDebugMsg(I2S(total))
    else
        BJDebugMsg("Please use numbers greater than zero.")
    endif
endfunction

The functions perform the same, but the code is slightly different. I know that for a measly short function, it probably doesn't matter how do I do it, but when code starts getting awfully long... Er, I don't know. Am I just being too nitpicky about this? I want to know what others think about this.
12-24-2013, 10:22 PM#2
PurgeandFire111
I'll usually do it at the start of the function after the local declarations. I'll do whatever checks I need to do, and then throw a message or add a return to leave the function.

Collapse JASS:
function AddTwoNumbers takes integer x, integer y returns nothing 
    local integer total

    // boundary checks
    if (x <= 0) or (y <= 0) then
        call BJDebugMsg("Please use numbers greater than zero.")
        return
    endif

    set total = x + y
    call BJDebugMsg(I2S(total))
endfunction
12-25-2013, 02:47 AM#3
Anitarf
Quote:
Originally Posted by PurgeandFire111
Collapse JASS:
function AddTwoNumbers takes integer x, integer y returns nothing 
    local integer total

    // boundary checks
    if (x <= 0) or (y <= 0) then
        call BJDebugMsg("Please use numbers greater than zero.")
        return
    endif

    set total = x + y
    call BJDebugMsg(I2S(total))
endfunction
Same here.
12-25-2013, 05:58 AM#4
TriggerHappy
Quote:
Originally Posted by PurgeandFire111
I'll usually do it at the start of the function after the local declarations. I'll do whatever checks I need to do, and then throw a message or add a return to leave the function.

I assume that it's more efficient that way.

Although I do the checks first just because.. that's how I do it
12-30-2013, 10:49 AM#5
WaterKnight
Top, because then you can immediately complete the (exception) case and write a simple return -> the rest does not have to be indented.