teepee
teepee

Reputation: 2714

Check errors before function call or within function?

I am curious to know about what is considered to be the best method to check for errors before running a function. Is it best to make a check before the function is called or within the function itself?

For example, a simplified version of what I'm working on involves this Click subroutine:

Private Sub MyButton1_Click()
    For j = 1 to 3
        CreateChart Sheets(j)
    Next j
End Sub

Where the function it calls is defined like so:

Function CreateChart(Sht As Worksheet) As Boolean
    Set ChtObj = Sht.ChartObjects.Add(40, 40, 600, 300)
    Set Cht = ChtObj.Chart
...
End Function

I am dealing with code with multiple modules and many situations where certain checks need to be performed before the function can successfully run. Is it most appropriate to put a check within the loop in the Click sub routine, something like:

If DoesSheetExist(Sheets(j)) Then CreateChart(Sheets(j))

Or best to put it within the function like:

If Not DoesSheetExist(Sht) Then CreateChart = False: Exit Function

Currently I have a little bit of each practice scattered throughout the code and I would like to clean it up. Is it best to run this check outside the function or within?

Upvotes: 1

Views: 122

Answers (1)

enderland
enderland

Reputation: 14145

I am dealing with code with multiple modules and many situations where certain checks need to be performed before the function can successfully run. Is it most appropriate to put a check within the loop in the Click sub routine, something like:

Generally I would avoid repeating the same check in your main code if you are going to do it for each.

In a situation where I have say 4 checks, and I would like to know which issue is causing the problem when the function fails by means of a MsgBox, then sometimes I leave the checks outside of the loops in the main code so that only one notification appears. Is there a way to have the checks within the functions but only report the issues once?

It sounds like you have a handful of checks will be the same for each CreateChart method. I would do something like:

Sub mainSub()
    If validateCreateChart Then
        CreateChart
    End If
End Sub

Sub CreateChart()
    On Error Goto errHandler
    'do stuff
    exit sub
    errHandler:
        msgbox  "Unexpected error: " & err.description
End Sub

Function validateCreateChart() As Boolean


    If ConditionOne Then
        validateCreateChart = False
        MsgBox "error condition 1"
        Exit Function
    End If


    If ConditionTwo Then
        validateCreateChart = False
        MsgBox "error condition 2"
        Exit Function
    End If

    'etc

End Function

You don't want to have your logic for errors/prompts scattered everywhere. Especially if you are doing the same checks and will have the same error prompts for each of them.

Don't leave a sub/function without some error handling or you will find yourself regretting it when some "this will never happen" circumstance happens.

Upvotes: 2

Related Questions