In this recipe, we will learn how to implement an error trap inside a function or subroutine.
From the File menu, navigate to New | Function Library... or use the Alt + Shift + N shortcut. Name the new function library ErrHandling_Func.vbs
. You can use any other name, or reuse an existing function library. Do not forget to ensure that the library is attached to the test through Resources (File | Settings | Resources).
The technique is very simple. First, within your function, identify the line or lines of code that carry the potential of raising an exception (for example, an unhandled error). For instance, we write the following simple function to perform a division operation:
Function DblDivideXByY(x, y) 'Return the result of the division as a Double DivideXByY=CDbl(x/y) End function
The problem with the preceding code is that it assumes a priori that the parameters passed to the function are valid. However...